CDK documentation states that if you supply stack name(s) to CLI commands like cdk synth
, cdk list
or cdk deploy
– it will synthesize only the stacks you requested. But in reality this is not the case, CDK will always synthesize all stacks – and it may lead to unintended consequences.
Let’s say you have following stack declarations in your lib/my-stacks.ts
code:
import * as cdk from 'aws-cdk-lib';
export class Stack1 extends cdk.Stack {};
export class Stack2 extends cdk.Stack {};
export class AnotherStack extends cdk.Stack {};
export class YerAnotherStack extends cdk.Stack {};
And in your app’s entry point bin/my-stacks.ts
you instantiate those stacks:
import * as cdk from 'aws-cdk-lib';
import * as stacks from '../lib/my-stacks'
const app = new cdk.App();
new stacks.Stack1(app, "Stack1");
new stacks.Stack2(app, "Stack2");
new stacks.AnotherStack(app, "AnotherStack");
new stacks.YerAnotherStack(app, "YetAnotherStack");
And then issue a CLI command to synthesize stacks, but you only want to synthesize “Stack1” and “AnotherStack”:
cdk synth Stack1 AnotherStack
The command output would have you believe that only 2 stacks were synthesized:
Successfully synthesized to ./cdk.out
Supply a stack id (AnotherStack, Stack1) to display its template.
Continue reading →