如何将cloudformation模板的资源逻辑ID获取到cdk-python

时间:2020-08-10 08:53:33

标签: python-3.x amazon-web-services aws-cdk cdk

我正在将cloudformation模板迁移到CDK。唯一的问题是我不知道如何从cloudformation模板到CDK获取相同的父堆栈逻辑ID。请帮忙!谢谢!还是有一种方法可以替代cdk生成的逻辑ID?当将模板迁移到CDK而没有Cfn时,由于逻辑ID的更改而实际上删除并重新创建资源,这将非常有用。

        from aws_cdk.core import Stack, Construct, StackProps
        import aws_cdk.aws_cloudformation as cfn
        import aws_cdk.aws_s3 as s3
        import aws_cdk.core as core
        import aws_cdk.cloudformation_include as cfn_inc
        
        
        
        class MyNestedStack1(cfn.NestedStack):
            def __init__(self, scope, id, *, parameters=None, timeout=None, notifications=None):
                super().__init__(scope, id)
                
                bucket = s3.Bucket.from_bucket_name(self, "S3Bucket", 
                   bucket_name = "sample-bucket-sample-tin-test")
                bucket_arn = core.Fn.get_att("S3Bucket", "Arn")
    
        
        class MyParentStack1 (Stack):
            def __init__(self, scope, id, *, description=None, env=None, stack_name=None, tags=None, synthesizer=None, terminationProtection=None):
                super().__init__(scope, id, stack_name='aws-stack-nightly')
    

                MyNestedStack(self, "AWSHealthStack")
    

如屏幕截图所示。 AWSHealthStack 是cf模板的逻辑ID。然后,当我将其部署到cdk时,它将创建另一个逻辑ID( AWSHealthStackNestedStackAWSHealthStackNestedStackResource7F23391A

enter image description here

在这里,我尝试了此代码,以便可以使用CFnInclude覆盖逻辑ID。但是我遇到了这个错误:

jsii.errors.JSIIError:缺少必需的属性 @ aws-cdk / cloudformation-include.CfnIncludeProps:templateFile

class MyParentStack(core.Stack):
    def __init__(self, scope, id, *, description=None, env=None, stack_name=None, tags=None, synthesizer=None, terminationProtection=None):
        super().__init__(scope, id, stack_name='aws-stack-nightly')

        stack = MyNestedStack(self, "AWSHealthStack1")
        cfn_template = cfn_inc.CfnIncludeProps(template_file="monitoring-template/aws-stack.yml",
            nested_stacks=[stack])
        cfn_template1 = cfn_inc.CfnInclude(self, "AWSHealthStack2", template_file="monitoring-template/aws-stack.yml",
            nested_stacks=cfn_template)
        cfn_stack = core.Fn.get_att("AWSHealthStack", "Arn")
        cfn_template1.override_logical_id("AWSHealthStack")

任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

我通过重命名逻辑ID来解决这个问题。

stack.rename_logical_id("AWSHealthNestedStackAWSHealthNestedStackResource7F23391A", "AWSHealthStack")

但是,如果有什么方法更好,将不胜感激。