有助于使用CDK中现有的CloudFormation模板的包

aws-cdk.cloudformation-include的Python项目详细描述


在CDK中包含云信息模板

---

cdk-constructs: Stable


这个模块包含一组类,它们的目标是促进工作 使用CDK中现有的云信息模板。 它可以看作是 ^{} class。在

基本用途

假设我们有一个具有现有模板的文件。 它可以是JSON格式的文件my-template.json

{"Resources":{"Bucket":{"Type":"AWS::S3::Bucket","Properties":{"BucketName":"some-bucket-name"}}}}

或者它可以采用YAML格式,在一个文件my-template.yaml

^{pr2}$

它可以包含在CDK应用程序中,代码如下:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826importaws_cdk.cloudformation_includeascfn_inccfn_template=cfn_inc.CfnInclude(self,"Template",template_file="my-template.json")

或者,如果模板使用YAML:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826cfn_template=cfn_inc.CfnInclude(self,"Template",template_file="my-template.yaml")

Note:不同的YAML解析器有时不同意什么是有效的YAML。 如果包含模板时出现YAML异常, 尝试将其转换为JSON,并包含该文件。 如果您要从CloudFormation AWS控制台下载模板, 您可以通过单击“在设计器中查看”轻松获得JSON格式的文件 “模板”选项卡上的按钮- 进入Designer后,在“Choose template language”中选择JSON 底部窗格上的单选按钮。在

这将把my-template.json/my-template.yaml中的所有资源添加到CDK应用程序中, 保留模板文件中的原始逻辑ID。在

注意,这个包含进程将not执行任何 CloudFormation transforms- 包括Serverless transform。在

从包含的模板中的任何资源都可以通过从模板中引用它的逻辑ID来检索。 如果您知道与该资源对应的CDK对象的类, 可以将返回的对象强制转换为正确的类型:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826importaws_cdk.aws_s3ass3cfn_bucket=cfn_template.get_resource("Bucket")

请注意,CloudFormation模式的最新版本中没有任何资源 在发布您所依赖的本模块版本时, 包括Custom Resources, 将作为类CfnResource的实例返回, 因此不能强制转换为其他资源类型。在

对该资源所做的任何修改都将反映在生成的CDK模板中; 例如,可以更改bucket的名称:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826cfn_bucket.bucket_name="my-bucket-name"

您也可以在定义其他构造时引用资源, 包括更高层的 (名字不以Cfn开头的人), 例如:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826importaws_cdk.aws_iamasiamrole=iam.Role(self,"Role",assumed_by=iam.AnyPrincipal())role.add_to_policy(iam.PolicyStatement(actions=["s3:*"],resources=[cfn_bucket.attr_arn]))

如果需要,还可以将CloudFormation资源转换为更高级别的资源 通过导入资源:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826bucket=s3.Bucket.from_bucket_name(self,"L2Bucket",cfn_bucket.ref)

非资源模板元素

除了资源, 还可以检索和修改所有其他模板元素:

  • Parameters

    # Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826importaws_cdk.coreascoreparam=cfn_template.get_parameter("MyParameter")# mutating the parameterparam.default="MyDefault"
  • Conditions

    # Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826importaws_cdk.coreascorecondition=cfn_template.get_condition("MyCondition")# mutating the conditioncondition.expression=core.Fn.condition_equals(1,2)
  • Mappings

    # Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826importaws_cdk.coreascoremapping=cfn_template.get_mapping("MyMapping")# mutating the mappingmapping.set_value("my-region","AMI","ami-04681a1dbd79675a5")
  • Service Catalog template Rules

    # Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826importaws_cdk.coreascorerule=cfn_template.get_rule("MyRule")# mutating the rulerule.add_assertion(core.Fn.condition_contains(["m1.small"],my_parameter.value),"MyParameter has to be m1.small")
  • Outputs

    # Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826importaws_cdk.coreascoreoutput=cfn_template.get_output("MyOutput")# mutating the outputoutput.value=cfn_bucket.attr_arn
  • Hooks for blue-green deployments

    # Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826importaws_cdk.coreascorehook=cfn_template.get_hook("MyOutput")# mutating the hookcode_deploy_hook=hookcode_deploy_hook.service_role=my_role.role_arn

参数替换

如果现有模板使用CloudFormation参数, 您可能希望移除它们,以支持构建时值。 您可以使用parameters属性执行此操作:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826inc.CfnInclude(self,"includeTemplate",template_file="path/to/my/template",parameters={"MyParam":"my-value"})

这将用字符串'my-value'替换对MyParam的所有引用, 并且MyParam将从模板的“Parameters”部分删除。在

嵌套堆栈

此模块还支持使用nested stacks的模板。在

例如,如果您有以下父模板:

{"Resources":{"ChildStack":{"Type":"AWS::CloudFormation::Stack","Properties":{"TemplateURL":"https://my-s3-template-source.s3.amazonaws.com/child-stack.json"}}}}

其中,https://my-s3-template-source.s3.amazonaws.com/child-stack.json指向的子模板是:

{"Resources":{"MyBucket":{"Type":"AWS::S3::Bucket"}}}

您可以同时包含父堆栈, 以及CDK应用程序中的嵌套堆栈,如下所示:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826parent_template=inc.CfnInclude(self,"ParentStack",template_file="path/to/my-parent-template.json",load_nested_stacks={"ChildStack":{"template_file":"path/to/my-nested-template.json"}})

这里,path/to/my-nested-template.json 表示从嵌套堆栈的原始模板URL下载的模板文件在磁盘上的路径 (https://my-s3-template-source.s3.amazonaws.com/child-stack.json)。 在CDK应用程序中, {a12}将转换为一个文件}, 以及嵌套堆栈资源的TemplateURL属性 将被修改以指向该资产。在

可以使用getNestedStack方法访问包含的嵌套堆栈:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826included_child_stack=parent_template.get_nested_stack("ChildStack")child_stack=included_child_stack.stackchild_template=included_child_stack.included_template

现在您可以引用ChildStack中的资源, 像其他任何包含的t一样修改它们就业:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826cfn_bucket=child_template.get_resource("MyBucket")cfn_bucket.bucket_name="my-new-bucket-name"role=iam.Role(child_stack,"MyRole",assumed_by=iam.AccountRootPrincipal())role.add_to_policy(iam.PolicyStatement(actions=["s3:GetObject*","s3:GetBucket*","s3:List*"],resources=[cfn_bucket.attr_arn]))

您还可以在创建CfnInclude对象之后包含嵌套堆栈, 而不是在建筑上:

^{pr21}$

将云信息模板作为构造出售

在许多情况下,现有的云信息模板不是完整的应用程序, 但更像是专门的片段,实现特定的模式或最佳实践。 如果你有这样的模板, 您可以使用CfnInclude类将它们作为CDK构造进行销售:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826importpathaspathclassMyConstruct(Construct):def__init__(self,scope,id):super().__init__(scope,id)# include a template inside the Constructcfn_inc.CfnInclude(self,"MyConstruct",template_file=path.join(__dirname,"my-template.json"),preserve_logical_ids=False)

{cd20>注意参数- 它确保所有包含的模板元素的逻辑ID都使用CDK的算法重新命名, 确保它们在应用程序中是唯一的。 没有传递参数, 在同一个堆栈中实例化MyConstruct两次将导致重复的逻辑标识。在

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

推荐PyPI第三方库


热门话题
我们可以使用java for loop with switch语句。。?   java如何从具有循环关系的数据集生成树?   使用s:mvcUrl标记时java Spring启动问题   javascript Wicket调色板水平滚动条在firefox中不显示   java如何清理Maven插件绑定?   java将文件从设备发送到计算机不起作用(文件被破坏)   Java GPU编程的性能   java字符串标记器问题   连接Java LDAP断开连接   java Android GridView将无法确定正确的位置   java AlarmManager在指定时间未显示toast消息   MVC中的java测试外观|断言返回类型   logcat上的java Android运行时错误   java Building Workspace在Eclipse中遇到错误   堆栈溢出尾部递归遗传算法抛出“awteventque0”java。StackOverflowerr语言   java ArrayOutOfBounds测试代码不工作   java有没有像Moodle这样流行的课程管理系统?   java如何从jsp获取xml文件作为响应