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

2024-05-23 21:14:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在将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: Missing required properties for @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")

Tags: namecoreimportselfnoneawsidinit
1条回答
网友
1楼 · 发布于 2024-05-23 21:14:14

我通过重命名逻辑id找到了答案

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

但如果有更好的方法,我们将不胜感激

相关问题 更多 >