0

I am migrating the cloudformation template to CDK. The only problem is I don't know how to get the same logical ID of parent stack from the cloudformation template to CDK. Any help please! Thank you! or is there a way to override cdk generated logical id? This will be really helpful when migrating templates to CDK w/o Cfn actually deleting and recreating the resource due to change in logical 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")
    

As you can see on the screenshot. The AWSHealthStack is the logical ID of the cf template. Then when I deployed that to cdk it creates another logical id (AWSHealthStackNestedStackAWSHealthStackNestedStackResource7F23391A)

enter image description here

Well in here I tried this code so I can override the logical id using CFnInclude. But I got this error:

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")
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Tine
  • 37
  • 1
  • 7
  • [typescript](https://stackoverflow.com/questions/57213637/aws-cdk-fixed-logical-ids) has overrideLogicalId('abc') – lloyd Aug 11 '20 at 07:28
  • Yes but I prefer using the python. In python which is this one `override_logical_id("AWSHealthStack")`. Is there a way that I can used that on **class MyParentStack1 (Stack)**? – Tine Aug 11 '20 at 09:10
  • Have you done through POC on CDK? Every resource I used had some defects, even breaking change of CLI prerequisite). Please make sure the resource you intended to use works. Every 2, 3 weeks a new release coming out asking to update. – mon Aug 15 '20 at 04:47
  • Currently approx 1300 issues open in https://github.com/aws/aws-cdk/issues. – mon Aug 15 '20 at 04:49

1 Answers1

0

I figured it out by renaming the logical id.

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

But if there is anything that has a better approach would be much appreciated.

Tine
  • 37
  • 1
  • 7