如何获取cloudformation模板的资源逻辑id到cdk - python
How to get the resource logical id of cloudformation template to cdk - python
我正在将 cloudformation 模板迁移到 CDK。唯一的问题是我不知道如何从 cloudformation 模板到 CDK 获取父堆栈的相同逻辑 ID。请帮忙!谢谢!或者有没有办法覆盖 cdk 生成的逻辑 id?这在将模板迁移到 CDK 时非常有用 w/o 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)
好吧,我在这里尝试了这段代码,因此我可以使用 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")
我通过重命名逻辑 ID 弄明白了。
stack.rename_logical_id("AWSHealthNestedStackAWSHealthNestedStackResource7F23391A", "AWSHealthStack")
但如果有任何更好的方法,将不胜感激。
我正在将 cloudformation 模板迁移到 CDK。唯一的问题是我不知道如何从 cloudformation 模板到 CDK 获取父堆栈的相同逻辑 ID。请帮忙!谢谢!或者有没有办法覆盖 cdk 生成的逻辑 id?这在将模板迁移到 CDK 时非常有用 w/o 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)
好吧,我在这里尝试了这段代码,因此我可以使用 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")
我通过重命名逻辑 ID 弄明白了。
stack.rename_logical_id("AWSHealthNestedStackAWSHealthNestedStackResource7F23391A", "AWSHealthStack")
但如果有任何更好的方法,将不胜感激。