有助于使用CDK中现有的CloudFormation模板的包
aws-cdk.cloudformation-include的Python项目详细描述
在CDK中包含云信息模板
---这个模块包含一组类,它们的目标是促进工作
使用CDK中现有的云信息模板。
它可以看作是
^{
基本用途
假设我们有一个具有现有模板的文件。
它可以是JSON格式的文件my-template.json
:
{"Resources":{"Bucket":{"Type":"AWS::S3::Bucket","Properties":{"BucketName":"some-bucket-name"}}}}
或者它可以采用YAML格式,在一个文件my-template.yaml
:
它可以包含在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)
非资源模板元素
除了资源, 还可以检索和修改所有其他模板元素:
- 在
# 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"
在 - 在
# 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)
在 - 在
# 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")
在 - 在
# 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
对象之后包含嵌套堆栈,
而不是在建筑上:
将云信息模板作为构造出售
在许多情况下,现有的云信息模板不是完整的应用程序,
但更像是专门的片段,实现特定的模式或最佳实践。
如果你有这样的模板,
您可以使用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
两次将导致重复的逻辑标识。在
- 项目
标签: