Skip to content

Instantly share code, notes, and snippets.

@koheyamada
Created April 9, 2018 03:15
Show Gist options
  • Select an option

  • Save koheyamada/162c662bc88ecb2feccd6f5bcc28b261 to your computer and use it in GitHub Desktop.

Select an option

Save koheyamada/162c662bc88ecb2feccd6f5bcc28b261 to your computer and use it in GitHub Desktop.
troposphereでCloudFormationのテンプレートを作成する。 ref: https://qiita.com/kooohei/items/17f6fa6facacfd0201b6
>>> from troposphere import Ref, Template
>>> import troposphere.ec2 as ec2
>>> t = Template()
>>> instance = ec2.Instance("myinstance", ImageId="ami-a77c30c1", InstanceType="t1.micro")
>>> t.add_resource(instance)
<troposphere.ec2.Instance object at 0x107c427b8>
>>> print(t.to_yaml())
Resources:
myinstance:
Properties:
ImageId: ami-a77c30c1
InstanceType: t1.micro
Type: AWS::EC2::Instance
>>> from troposphere import Ref, Template
>>> import troposphere.ec2 as ec2
>>> t = Template()
>>> instance = t.add_resource(ec2.Instance("myinstance", ImageId="ami-a77c30c1", InstanceType="t1.micro"))
>>> Ref(instance)
<troposphere.Ref object at 0x10b2647f0>
>>> print(t.to_yaml())
Resources:
myinstance:
Properties:
ImageId: ami-a77c30c1
InstanceType: t1.micro
Type: AWS::EC2::Instance
>>> import troposphere.ec2 as ec2
>>> ec2.Instance("ec2instance", image="i-XXXX")
Traceback (most recent call last):
・・・
AttributeError: AWS::EC2::Instance object does not support attribute image
>>> from troposphere import Template
>>> import troposphere.ec2 as ec2
>>> t = Template()
>>> t.add_resource(ec2.Instance("ec2instance", InstanceType="m3.medium"))
<troposphere.ec2.Instance object at 0x110965780>
>>> print(t.to_json())
Traceback (most recent call last):
・・・
ValueError: Resource ImageId required in type AWS::EC2::Instance (title: ec2instance)
>>> from troposphere import Ref, Template
>>> import troposphere.ec2 as ec2
>>> t = Template()
>>> instance = ec2.Instance("myinstance")
>>> instance.ImageId = "ami-a77c30c1"
>>> instance.InstanceType = "t1.micro"
>>> t.add_resource(instance)
<troposphere.ec2.Instance object at 0x1057597b8>
>>> print(t.to_json())
{
"Resources": {
"myinstance": {
"Properties": {
"ImageId": "ami-a77c30c1",
"InstanceType": "t1.micro"
},
"Type": "AWS::EC2::Instance"
}
}
}
>>> print(t.to_yaml())
Resources:
myinstance:
Properties:
ImageId: ami-a77c30c1
InstanceType: t1.micro
Type: AWS::EC2::Instance
$ pip install troposphere
Collecting troposphere
Downloading troposphere-2.2.1.tar.gz (115kB)
100% |████████████████████████████████| 122kB 3.0MB/s
Collecting cfn_flip>=0.2.5 (from troposphere)
Downloading cfn_flip-1.0.3.tar.gz
Collecting Click (from cfn_flip>=0.2.5->troposphere)
Downloading click-6.7-py2.py3-none-any.whl (71kB)
100% |████████████████████████████████| 71kB 4.4MB/s
Requirement already satisfied: PyYAML in /Users/kohei/.pyenv/versions/3.6.2/lib/python3.6/site-packages (from cfn_flip>=0.2.5->troposphere)
Requirement already satisfied: six in /Users/kohei/.pyenv/versions/3.6.2/lib/python3.6/site-packages (from cfn_flip>=0.2.5->troposphere)
Installing collected packages: Click, cfn-flip, troposphere
Running setup.py install for cfn-flip ... done
Running setup.py install for troposphere ... done
Successfully installed Click-6.7 cfn-flip-1.0.3 troposphere-2.2.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment