Stack names are not honoured when creating a nested stack
Stack names are not honoured when creating a multi-stack, multi-environment CDK application
Reproduction Steps
I am trying to create a CDK app with multiple stacks that is deployed to multiple environments, e.g.
#!/usr/bin/env node
import * as cdk from '@aws-cdk/core';
import * as s3 from '@aws-cdk/aws-s3';
import * as lambda from '@aws-cdk/aws-lambda';
class PersistenceStack extends cdk.Stack {
public readonly bucket: s3.Bucket;
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
this.bucket = new s3.Bucket(this, 'bucket');
}
}
interface ApplicationStackProps extends cdk.StackProps {
bucket: s3.Bucket;
}
class ApplicationStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props: ApplicationStackProps) {
super(scope, id, props);
const myLambda = new lambda.Function(this, 'my-lambda', {
runtime: lambda.Runtime.NODEJS_12_X,
code: new lambda.AssetCode('my-lambda'),
handler: 'index.handler'
});
props.bucket.grantReadWrite(myLambda);
}
}
class MyApp extends cdk.Construct {
constructor(scope: cdk.Construct, id: string, props: cdk.StackProps) {
super(scope, id);
const persistenceStack = new PersistenceStack(this, 'persistence-stack', {
...props,
description: 'persistence stack',
stackName: `${id}-persistence-stack`,
});
const applicationStack = new ApplicationStack(this, 'application-stack', {
...props,
description: 'application stack',
stackName: `${id}-application-stack`,
bucket: persistenceStack.bucket,
});
applicationStack.addDependency(persistenceStack);
}
}
const app = new cdk.App();
new MyApp(app, `test`, { env: { account: '111111111111', region: 'eu-west-1' } });
new MyApp(app, `prod`, { env: { account: '222222222222', region: 'eu-west-1' } });
Expected result
I expected to see "nice" (or at least predictable) stack names when executing $ npx cdk synth since I have specified the stackName properties when calling the constructors, e.g.
Successfully synthesized to [...]/my-app/cdk.out
Supply a stack id (prodpersistencestack, testpersistencestack, prodapplicationstack, testapplicationstack) to display its template.
Actual result
Stack names ends with random / hashed characters similar to:
Successfully synthesized to [...]/my-app/cdk.out
Supply a stack id (prodpersistencestackFE36DF49, testpersistencestack6C35C777, prodapplicationstackA0A96586, testapplicationstackE19450AB) to display its template.
Motivation
I need "nice" (or at least predictable) stack names to feed into the next step our CI/CD pipeline so that I can configure the build server to deploy the CDK app, e.g.
npx cdk deploy \
--app cdk.out \
--require-approval never \
testpersistencestack
npx cdk deploy \
--app cdk.out \
--require-approval never \
testapplicationstack
Environment
- CLI Version: 1.21.1 (build 842cc5f)
- Framework Version: 1.21.1
- OS: macOS Catalina Version 10.15.2
- Language: TypeScript
This is :bug: Bug Report
This is by design. Stack name is the physical name of the stack and there could be multiple stacks with the same name (e.g. when deployed to different environments). Therefore the identification of stack artifacts in the CLI is based on the stack's artifactId which is a deterministic and consistent value derived from the stack's construct node path.
If you are synthesizing your CI/CD pipeline through the CDK, you can use stack.artifactId to obtain this value. If you configure your CI/CD pipeline elsewhere, you should feel comfortable using these artifact IDs. As long as the construct tree structure doesn't change, this ID not change.
Alternatively, you could read the file manifest.json in cdk.out, which contains in formation about the stacks in your assembly. In there you will be able to find both the artifact ID (the key to the map) and the configured stack name. You could then produce a reverse lookup if that's what you are after.
You can see #4895 for details.
Closing for now.
@eladb Thanks for taking the time to writing this up. As you mentioned, our build server is indeed creating artifacts based on the cdk.out so ended up using jq to parse the manifest.json file as you suggested, added below for future reference:
stackName= # get stack name from somewhere
stackId=$(jq -r --arg stackName "$stackName" \
'.artifacts
| to_entries[]
| select(.value.properties.stackName == $stackName)
| .key
' \
< "./cdk.out/manifest.json" \
)
npx cdk deploy \
--app cdk.out \
--require-approval never \
"$stackId"
With this approach, we can have $stackName as parameter to our CI/CD deployment script
I have some follow-up questions, but I am not sure which is the best way of communicating them. In the end, I guess I would like to see some additions in the developer guide.
- You write that "the stack's artifactId which is a deterministic and consistent value derived from the stack's construct node path" and "As long as the construct tree structure doesn't change, this ID not change". I guess that in this particular example it refers to
MyAppConstruct and the invocations ofnew PersistenceStack(this, ...)andnew PersistenceStack(this, ...)and that they are passed as constructor parameters? - As a side effect, it also means that if I modify the
MyAppconstruct I will deploy a new CloudFormation stack instead of modifying an existing stack? - Perhaps you have already been considering it, but one suggestion to improve the deployment experience could be to add an entry in the
manifest.jsonthat mapsaccount.region.stackNametostackId, e.g.
{
"111111111111": {
"eu-west-1": {
"first-stack-name": "first-stack-id",
"second-stack-name": "second-stack-id"
}
},
// more accounts
}
That way, there is no need for the reverse mapping. Additionally, that enables you to extend the CDK deploy command to something like
npx cdk deploy \
--app cdk.out \
--require-approval never \
--account 111111111111 \
--region eu-west-1 \
first-stack-name second-stack-name
This is straight-up bullshit that you can not simply force "normal" ids in your cdk application. Bad design choice.
Technically, it's horsesht, not bulls#t. Bullsh#$ would imply it's a lie.
The autogenerated stack names are really quite horrible for someone who is viewing them in CloudFormation Console. Instead of something intelligible like my-app-dev-ui the name is forced as my-app-dev-uiNestedStackuiNestedStackResource522F576C-5ULOQ2MK4GY2 and there doesn't seem any way to fix it with a prop attribute.
We will be releasing a new view in CloudFormation soon which will help with this. 👀
+1
+1
+1
because of this behaviour you get into errors like this one

We will be releasing a new view in CloudFormation soon which will help with this. 👀
Any update on this?
We will be releasing a new view in CloudFormation soon which will help with this. 👀
Any update on this?
I believe this is now released: https://aws.amazon.com/about-aws/whats-new/2022/09/aws-cloud-development-kit-cdk-announces-cdk-construct-tree-view-cloudformation-console/