用pythonic方式编排aws资源。

troposphere-mate的Python项目详细描述


Documentation Statushttps://travis-ci.org/MacHu-GWU/troposphere_mate-project.svg?branch=masterhttps://codecov.io/gh/MacHu-GWU/troposphere_mate-project/branch/master/graph/badge.svghttps://img.shields.io/pypi/v/troposphere_mate.svghttps://img.shields.io/pypi/l/troposphere_mate.svghttps://img.shields.io/pypi/pyversions/troposphere_mate.svghttps://img.shields.io/badge/STAR_Me_on_GitHub!--None.svg?style=social
https://img.shields.io/badge/Link-Document-blue.svghttps://img.shields.io/badge/Link-API-blue.svghttps://img.shields.io/badge/Link-Source_Code-blue.svghttps://img.shields.io/badge/Link-Install-blue.svghttps://img.shields.io/badge/Link-GitHub-blue.svghttps://img.shields.io/badge/Link-Submit_Issue-blue.svghttps://img.shields.io/badge/Link-Request_Feature-blue.svghttps://img.shields.io/badge/Link-Download-blue.svg

欢迎使用troposphere_mate文档

troposphere_mate一个高效的pythonic cloudformation编排工具

troposphere是一个很好的python库,允许您在python类中定义aws cloudformation资源。但是由于它的实现,idle不能使用属性auto hint,类型hint也不能工作。

troposphere_mate提供与troposphere完全相同的api,并附带properties auto hinttype hinttroposphere_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

这里是空闲时的样子

https://user-images.githubusercontent.com/6800411/60903484-686b1b80-a23f-11e9-8d20-22c989339cd0.pnghttps://user-images.githubusercontent.com/6800411/60776028-e40d8100-a0f6-11e9-9cae-98af25cbd9b7.pnghttps://user-images.githubusercontent.com/6800411/60776079-3484de80-a0f7-11e9-81b8-c4b2f1c4b45e.png

当然可以:

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),])

如果您想将自动关联逻辑贡献给troposphere_mate,请提交issue或帮助我改进。这是一张example

Partial Deployment

在大多数情况下,最终你的云形成模板会变得很大。在开发和部署中有一些常见的用例:

  1. 要重用大型架构师设计中的aws资源,只部署选定的aws资源,而不编辑模板。
  2. 在进行开发或调试时,您希望逐步部署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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java Requestcontextholder在spring 4中具有并发访问权限。IBMWebSphere上的x Web应用程序?   java如何下载、设置和使用Eclipse?   java如何组合这些mysql语句   java JDBC无法连接到openshift上的mysql数据库   如果存在允许正确处理的重载,java对于方便的方法来说是否可行?   使用hibernate序列的java Spring MVC不存在   具有路径的java Selenium ChromeDriver负载扩展问题   读一本书。java中的java文件   退出队列时,Java队列程序结果为空   Java lambda返回带有重复代码问题的列表   java使用意图从其他活动传递数据并在listview中显示   java如何在java中创建JSON输出   java Android:在不破坏或暂停活动的情况下关闭显示   支持Android电视和手机的java多apk   关于Java应用程序测试和调试的一组问题   如何在JavaSE中使用jdbcRealmShiro进行授权   在java中是否有一个无异常检查的URL解析实用程序?   当页面上有多个相同类型的元素时,java会选择一个特定的元素   递归需要帮助发现java代码中的缺陷