troposphere
关于
troposphere – 创建AWS云形式描述的库
troposphere库可以通过编写Python代码来描述AWS资源来更轻松地创建AWS CloudFormation JSON。 troposphere还通过热量提供了对开放式空间资源的一些基本支持。
为了促进捕获云形式或JSON错误,图书馆具有属性和类型检查,其中内置了这些库。
安装
可以通过发出Python的PIP分配系统来安装troposphere :
$ pip install troposphere
使用AWAC安装troposphere (建议的软依赖):
$ pip install troposphere [policy]
另外,您可以使用setup.py通过克隆此存储库和发行来安装:
$ python setup.py install # you may need sudo depending on your python installation
例子
创建实例的一个简单示例将看起来像这样:
troposphere import Ref, Template
>>> import troposphere .ec2 as ec2
>>> t = Template()
>>> instance = ec2.Instance(\”myinstance\”)
>>> instance.ImageId = \”ami-951945d0\”
>>> instance.InstanceType = \”t1.micro\”
>>> t.add_resource(instance)
< troposphere .ec2.Instance object at 0x101bf3390>
>>> print(t.to_json())
{
\”Resources\”: {
\”myinstance\”: {
\”Properties\”: {
\”ImageId\”: \”ami-951945d0\”,
\”InstanceType\”: \”t1.micro\”
},
\”Type\”: \”AWS::EC2::Instance\”
}
}
}
>>> print(t.to_yaml())
Resources:
myinstance:
Properties:
ImageId: ami-951945d0
InstanceType: t1.micro
Type: AWS::EC2::Instance\”>
> >> from troposphere import Ref , Template > >> import troposphere . ec2 as ec2 > >> t = Template () > >> instance = ec2 . Instance ( \"myinstance\" ) > >> instance . ImageId = \"ami-951945d0\" > >> instance . InstanceType = \"t1.micro\" > >> t . add_resource ( instance ) < troposphere . ec2 . Instance object at 0x101bf3390 > > >> print ( t . to_json ()) { \"Resources\" : { \"myinstance\" : { \"Properties\" : { \"ImageId\" : \"ami-951945d0\" , \"InstanceType\" : \"t1.micro\" }, \"Type\" : \"AWS::EC2::Instance\" } } } > >> print ( t . to_yaml ()) Resources : myinstance : Properties : ImageId : ami - 951945 d0 InstanceType : t1 . micro Type : AWS :: EC2 :: Instance
另外,可以使用参数代替属性:
> >> instance = ec2 . Instance ( \"myinstance\" , ImageId = \"ami-951945d0\" , InstanceType = \"t1.micro\" )
> >> t . add_resource ( instance )
< troposphere . ec2 . Instance object at 0x101bf3550 >
and add_resource()返回对象,使其易于与Ref():
> >> instance = t . add_resource ( ec2 . Instance ( \"myinstance\" , ImageId = \"ami-951945d0\" , InstanceType = \"t1.micro\" ))
> >> Ref ( instance )
< troposphere . Ref object at 0x101bf3490 >
错误检查的示例(为了清楚起见,已删除了全反馈):
在AWS资源上设置了错误的属性:
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\”>
> >> 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
AWS资源属性的错误类型:
> >> ec2 . Instance ( \"ec2instance\" , ImageId = 1 ) Traceback ( most recent call last ): ... TypeError : ImageId is < type \'int\' > , expected < type \'basestring\' >
AWS资源缺少所需的属性:
troposphere import Template
>>> import troposphere .ec2 as ec2
>>> t = Template()
>>> t.add_resource(ec2.Subnet(\”ec2subnet\”, VpcId=\”vpcid\”))
< troposphere .ec2.Subnet object at 0x100830ed0>
>>> print(t.to_json())
Traceback (most recent call last):
…
ValueError: Resource CidrBlock required in type AWS::EC2::Subnet (title: ec2subnet)\”>
> >> from troposphere import Template > >> import troposphere . ec2 as ec2 > >> t = Template () > >> t . add_resource ( ec2 . Subnet ( \"ec2subnet\" , VpcId = \"vpcid\" )) < troposphere . ec2 . Subnet object at 0x100830ed0 > > >> print ( t . to_json ()) Traceback ( most recent call last ): ... ValueError : Resource CidrBlock required in type AWS :: EC2 :: Subnet ( title : ec2subnet )
当前支持的资源类型
- AWS资源类型
- OpenStack资源类型
复制单个实例样本看起来像这样
troposphere import Base64, FindInMap, GetAtt
from troposphere import Parameter, Output, Ref, Template
import troposphere .ec2 as ec2
template = Template()
keyname_param = template.add_parameter(Parameter(
\”KeyName\”,
Description=\”Name of an existing EC2 KeyPair to enable SSH \”
\”access to the instance\”,
Type=\”String\”,
))
template.add_mapping(\’RegionMap\’, {
\”us-east-1\”: {\”AMI\”: \”ami-7f418316\”},
\”us-west-1\”: {\”AMI\”: \”ami-951945d0\”},
\”us-west-2\”: {\”AMI\”: \”ami-16fd7026\”},
\”eu-west-1\”: {\”AMI\”: \”ami-24506250\”},
\”sa-east-1\”: {\”AMI\”: \”ami-3e3be423\”},
\”ap-southeast-1\”: {\”AMI\”: \”ami-74dda626\”},
\”ap-northeast-1\”: {\”AMI\”: \”ami-dcfa4edd\”}
})
ec2_instance = template.add_resource(ec2.Instance(
\”Ec2Instance\”,
ImageId=FindInMap(\”RegionMap\”, Ref(\”AWS::Region\”), \”AMI\”),
InstanceType=\”t1.micro\”,
KeyName=Ref(keyname_param),
SecurityGroups=[\”default\”],
UserData=Base64(\”80\”)
))
template.add_output([
Output(
\”InstanceId\”,
Description=\”InstanceId of the newly created EC2 instance\”,
Value=Ref(ec2_instance),
),
Output(
\”AZ\”,
Description=\”Availability Zone of the newly created EC2 instance\”,
Value=GetAtt(ec2_instance, \”AvailabilityZone\”),
),
Output(
\”PublicIP\”,
Description=\”Public IP address of the newly created EC2 instance\”,
Value=GetAtt(ec2_instance, \”PublicIp\”),
),
Output(
\”PrivateIP\”,
Description=\”Private IP address of the newly created EC2 instance\”,
Value=GetAtt(ec2_instance, \”PrivateIp\”),
),
Output(
\”PublicDNS\”,
Description=\”Public DNSName of the newly created EC2 instance\”,
Value=GetAtt(ec2_instance, \”PublicDnsName\”),
),
Output(
\”PrivateDNS\”,
Description=\”Private DNSName of the newly created EC2 instance\”,
Value=GetAtt(ec2_instance, \”PrivateDnsName\”),
),
])
print(template.to_json())\”>
# Converted from EC2InstanceSample.template located at: # http://aws.a*ma**zon.com/cloudformation/aws-cloudformation-templates/ from troposphere import Base64 , FindInMap , GetAtt from troposphere import Parameter , Output , Ref , Template import troposphere . ec2 as ec2 template = Template () keyname_param = template . add_parameter ( Parameter ( \"KeyName\" , Description = \"Name of an existing EC2 KeyPair to enable SSH \" \"access to the instance\" , Type = \"String\" , )) template . add_mapping ( \'RegionMap\' , { \"us-east-1\" : { \"AMI\" : \"ami-7f418316\" }, \"us-west-1\" : { \"AMI\" : \"ami-951945d0\" }, \"us-west-2\" : { \"AMI\" : \"ami-16fd7026\" }, \"eu-west-1\" : { \"AMI\" : \"ami-24506250\" }, \"sa-east-1\" : { \"AMI\" : \"ami-3e3be423\" }, \"ap-southeast-1\" : { \"AMI\" : \"ami-74dda626\" }, \"ap-northeast-1\" : { \"AMI\" : \"ami-dcfa4edd\" } }) ec2_instance = template . add_resource ( ec2 . Instance ( \"Ec2Instance\" , ImageId = FindInMap ( \"RegionMap\" , Ref ( \"AWS::Region\" ), \"AMI\" ), InstanceType = \"t1.micro\" , KeyName = Ref ( keyname_param ), SecurityGroups = [ \"default\" ], UserData = Base64 ( \"80\" ) )) template . add_output ([ Output ( \"InstanceId\" , Description = \"InstanceId of the newly created EC2 instance\" , Value = Ref ( ec2_instance ), ), Output ( \"AZ\" , Description = \"Availability Zone of the newly created EC2 instance\" , Value = GetAtt ( ec2_instance , \"AvailabilityZone\" ), ), Output ( \"PublicIP\" , Description = \"Public IP address of the newly created EC2 instance\" , Value = GetAtt ( ec2_instance , \"PublicIp\" ), ), Output ( \"PrivateIP\" , Description = \"Private IP address of the newly created EC2 instance\" , Value = GetAtt ( ec2_instance , \"PrivateIp\" ), ), Output ( \"PublicDNS\" , Description = \"Public DNSName of the newly created EC2 instance\" , Value = GetAtt ( ec2_instance , \"PublicDnsName\" ), ), Output ( \"PrivateDNS\" , Description = \"Private DNSName of the newly created EC2 instance\" , Value = GetAtt ( ec2_instance , \"PrivateDnsName\" ), ), ]) print ( template . to_json ())
社区
我们有一个Google组CloudTools-Dev,您可以在其中提出问题并与troposphere社区互动。总是欢迎问题和拉动请求!
许可
troposphere是根据BSD 2-CAREASE许可证获得许可的。请参阅troposphere完整许可文本的许可证。
