aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.5k stars 3.85k forks source link

(core): Nested Stacks have a crazy logical ID #18053

Open rix0rrr opened 2 years ago

rix0rrr commented 2 years ago

What is the problem?

Recently, we found out the construct id of the nested stack occurred twice in its logical id.

For example:

##### Parent infra stack ##### 
this.fdLifecycleCommonLambdas = new MyNestedStackConstruct(this, "MyNestedStackConstruct", {
 ...
 });

#### Nested stack #####
export class MyNestedStackConstruct extends NestedStack {
    ...
    constructor(parent: Construct, name: string, props: FdLifecycleCommonLambdasProps) {
        super(parent, name);
    }
    ...
}
#### Generated template ####
...
"Resources": {
    "MyNestedStackConstructNestedStackMyNestedStackConstructNestedStackResource3E193B32": {
            ^^^^^^^^^^^ oh no                                   ^^^^^^^^^^^^^^^^^^^^^^^
      "Type": "AWS::CloudFormation::Stack",
      "Properties": {}
    ...
}

Caused by the code here:

https://github.com/aws/aws-cdk/blob/2b6c2dadc039f7c4255f6864386ba1dd28b6fc4f/packages/%40aws-cdk/core/lib/nested-stack.ts#L123-L133

rix0rrr commented 2 years ago

Needs a feature flag to fix

RobReus commented 2 years ago

Is there anything happening on this issue? The crazy long names is bad as it is, but when doing a cross stack parameter reference between 2 nested stacks, the resulting parameter name is even crazier. Its currently not breaking anything for me, but I can't imagine this naming behavior is intended.

anupash147 commented 2 years ago

I would be interested too.. names are crazy long and am using cdk@2.26,

diranged commented 1 year ago

We just noticed this too - it would be great to clean this up and fix it.

konstantinj commented 1 year ago

re-posting my temporary solution for this here:

    getLogicalId(element: CfnElement): string {
        if (element.node.id.includes('NestedStackResource')) {
            return /([a-zA-Z0-9]+)\.NestedStackResource/.exec(element.node.id)![1] // will be the exact id of the stack
        }
        return super.getLogicalId(element)
    }

Place this inside your nested stacks to get a normal name like: ParentStackName-NestedStackName-LWJUNJMGDHO7

iamcrisb commented 1 year ago

re-posting my temporary solution for this here:

    getLogicalId(element: CfnElement): string {
        if (element.node.id.includes('NestedStackResource')) {
            return /([a-zA-Z0-9]+)\.NestedStackResource/.exec(element.node.id)![1] // will be the exact id of the stack
        }
        return super.getLogicalId(element)
    }

Place this inside your nested stacks to get a normal name like: ParentStackName-NestedStackName-LWJUNJMGDHO7

this should be the default behaviour

petro2050 commented 1 year ago

any update on this? it's driving me crazy that we can't provide a custom name for nested stacks.

coltenkrauter commented 8 months ago

Any updates on this?

germanozambelli commented 6 months ago

Any updates?

thorben-akent commented 3 months ago

Also interested in updates on this issue. We have had to implement the solution @konstantinj posted for every project on a client that is using nested stacks.

aa-exl commented 3 weeks ago

Any Updates on this ? We're trying to implement the workaround mentioned but would be great to have this as a feature