用pythonic方式编排aws资源。
troposphere-mate的Python项目详细描述
欢迎使用troposphere_mate文档
troposphere_mate是一个高效的pythonic cloudformation编排工具。
troposphere是一个很好的python库,允许您在python类中定义aws cloudformation资源。但是由于它的实现,idle不能使用属性auto hint,类型hint也不能工作。
troposphere_mate提供与troposphere完全相同的api,并附带properties auto hint和type hint。troposphere_mate只是troposphere上的一个薄包装层。任何troposphere_mate.AWSObject都只是troposphere.AWSObject的子类。
我的目标是100%api与troposphere兼容。基本上,您只需要将from troposphere import Template, Ref, Tags, GetAtt替换为from troposphere_mate import Template, Ref, Tags, GetAtt。
这里是空闲时的样子:
当然可以:
ec2=ec2.Instance(title="MyEc2Instance),InstanceType="t2.micro",Tags=Tags(Creator="MyName",Name="PlayGround",),...)
如何实现troposphere:
# content of troposphere.ec2.pyclassInstance(AWSObject):resource_type="AWS::EC2::Instance"props={'InstanceType':(basestring,False),'SubnetId':(basestring,False),'KeyName':(basestring,False),...}
如何实现troposphere_mate:
# content of troposphere_mate.ec2.pyclassInstance(troposphere.ec2.Instance,Mixin):def__init__(self,title,# type: strtemplate=None,# type: Templatevalidation=True,# type: boolInstanceType=NOTHING,# type: strSubnetId=NOTHING,# type: Union[str, AWSHelperFn]KeyName=NOTHING,# type: Union[str, AWSHelperFn]...**kwargs):...
Additional Powerful Features
Batch Tagging
有时您希望将一组公共标记应用于模板中定义的所有aws资源。trpoosphere_mate允许您:
- 对指定的aws资源列表或模板中的所有资源应用公共标记。
- 自定义标记创建逻辑函数,比如说基于资源类型。
- 允许您选择将existing tag合并到common tag或反向合并。
示例:
fromtroposphere_mateimportTemplate,ec2,Tags,fromfunctoolsimportpartialtpl=Template()my_vpc=ec2.VPC("MyVPC",template=tpl,CidrBlock="10.0.0.0/16",Tags=Tags(Creator="Alice"))my_sg=ec2.SecurityGroup("MySG",template=tpl,GroupDescription="My",GroupName="MySG",VpcId=Ref(my_vpc),)my_subnet=ec2.Subnet("MySubnet",template=tpl,CidrBlock="10.0.1.0/24",VpcId=Ref(my_vpc),)# custom logic to create tag if it is a SecurityGroupdefget_name(resource,project):ifresource.resource_type=="AWS::EC2::SecurityGroup":return"{}/sg/{}".format(project,resource.GroupName)common_tags=dict(Project="my-project",Name=functools.partial(get_name,project="my-project"),Creator="Bob",)# apply common tags to all aws resourcetpl.update_tags(common_tags,overwrite=False)asserttags_list_to_dct(tpl.to_dict()["Resources"]["MyVPC"]["Properties"]["Tags"])==dict(Project="my-project",Creator="Alice",)asserttags_list_to_dct(tpl.to_dict()["Resources"]["MySG"]["Properties"]["Tags"])==dict(Project="my-project",Name="my-project/sg/MySG",Creator="Bob",)
任何aws资源对象和模板对象都有一个实用方法.update_tags()
# by default overwrite = False, so common tags doesn't overwrite existing tags# update single resourcemy_ec2.update_tags({"Project":"my-project"})# update entire templatetpl.update_taggs({"Project":"my-project"})
Auto Reference
有时候,你只知道你需要将一个aws资源与另一个aws资源相关联,但是你 必须查找文档以找出要执行此操作的属性和语法。
例如,如果要将IAM角色、VPC子网、安全组与lambda函数关联。
假设您已经有了:
fromtroposphere_mateimportec2,awslambda,iamtpl=Template()iam_role=iam.Role(title="MyIamRole",template=tpl,RoleName="lambda-basic-execution",AssumeRolePolicyDocument={},)vpc=ec2.VPC(title="MyVPC",template=tpl,CidrBlock="10.53.0.0/16")public_subnet1=ec2.Subnet(title="PublicSubnet1",template=tpl,CidrBlock="10.53.0.0/16",VpcId=Ref(vpc))public_subnet2=ec2.Subnet(title="PublicSubnet2",template=tpl,CidrBlock="10.53.2.0/16",VpcId=Ref(vpc))sg=ec2.SecurityGroup(title="LambdaSG",template=tpl,GroupDescription="Just a SG")lbd_func=awslambda.Function(title="MyFunc",template=tpl,Code=awslambda.Code(S3Bucket="my-bucket",S3Key="0.0.1.zip",),Handler="my_func.handler",Role="arn:aws:iam::111122223333:role/todo",Runtime="python3.6")
使用troposphere_mate,您只需要这样做:
fromtroposphere_mateimportassociateassociate(lbd_func,iam_role)# order doesn't matter, associate(iam_role, lbd_func)associate(lbd_func,sg)associate(lbd_func,public_subnet1)associate(lbd_func,public_subnet2)
换句话说,您不需要记住属性和语法。
fromtroposphereimportReffromtroposphereimportawslambdalbd_func.Role=Ref(iam_role)lbd_func.VpcConfig=awslambda.VPCConfig(SecurityGroupIds=[Ref(sg)],SubnetIds=[Ref(public_subnet1),Ref(public_subnet2),])
Partial Deployment
在大多数情况下,最终你的云形成模板会变得很大。在开发和部署中有一些常见的用例:
- 要重用大型架构师设计中的aws资源,只部署选定的aws资源,而不编辑模板。
- 在进行开发或调试时,您希望逐步部署aws资源,而不是在一个命令中部署所有资源,而无需编辑模板。
troposphere_mate允许您在Metadata字段中定义aws资源的标签,然后可以使用Template.remove_resource_by_label(label="a label", label_field_in_metadata="labels")方法批量从模板中删除aws资源。
更重要的是,troposphere_mate允许您显式定义输出对象的依赖aws资源,因此当您删除该资源时,相关的输出将自动删除,这是本机cloudformation或troposphere不支持的。
示例:
fromtroposphere_mateimportec2,rdsclassLabels:tier1_vpc="tier1_vpc"vpc="vpc"sg="security_group"tier2_rds="tier2_rds"db_subnet_group="db_subnet_group"db_instance="db_instance"tpl=Template()vpc=ec2.VPC("VPC",template=tpl,Metadata={"labels":[Labels.tier1_vpc,Labels.vpc]},...)sg_ssh=ec2.SecurityGroup("SecurityGroupSSH",template=tpl,Metadata={"labels":[Labels.tier1_vpc,Labels.sg]},...)rds_db_subnet_group=rds.DBSubnetGroup("DBInstance",template=tpl,Metadata={"labels":[Labels.tier2_rds,Labels.db_subnet_group]})rds_instance=rds.DBInstance("DBInstance",template=tpl,Metadata={"labels":[Labels.tier2_rds,Labels.db_instance]})tpl.add_output(Output("VPC",Description="VPC ID",Value=Ref(vpc),Export=Export("vpc-id")),DependsOn=[vpc,],# specify the dependent AWS Resource, so when you remove the resource, related output will automatically removed),)assertlen(tpl.resources)==4assertlen(tpl.outputs)==1tpl.remove_resource_by_label(Labels.db_instance)assertlen(tpl.resources)==3tpl.remove_resource_by_label(Labels.tier2_rds)assertlen(tpl.resources)==2tpl.remove_resource_by_label(Labels.tier1_vpc)assertlen(tpl.resources)==0assertlen(tpl.outputs)==0
Install
troposphere_mate在pypi上发布,所以您只需要:
$ pip install troposphere_mate
要升级到最新版本:
$ pip install --upgrade troposphere_mate