Sub topic 3
Apps, stacks and the construct tree
This chapter assumes you can read basic TypeScript or Python and know what a CloudFormation stack is. It explains how a CDK program is organized, because that structure decides what gets deployed and what names your resources get.
Three roles: App, Stack, Construct
Every CDK program is built from constructs. A construct is a class from the constructs library that represents a piece of infrastructure.
- An App is the root of the tree. It holds stacks and has no parent.
- A Stack is the unit of deployment. Each stack becomes one CloudFormation template and one CloudFormation stack.
- Any other Construct (a bucket, a function, or your own grouping) lives inside a stack or inside another construct.
Each construct takes scope and id as its first two arguments. The scope is the parent, and the id must be unique among siblings. The this you pass inside a stack's constructor makes the stack the parent.
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as s3 from 'aws-cdk-lib/aws-s3';
class DataStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
new s3.Bucket(this, 'UploadsBucket');
}
}
const app = new cdk.App();
new DataStack(app, 'DataStack');
new DataStack(app, 'DataStackDev');
app.node.findAll().forEach((c) => console.log(c.node.path));Running this prints paths such as DataStack, DataStack/UploadsBucket and DataStack/UploadsBucket/Resource. The last one is the low-level CfnBucket that the bucket construct creates internally. The tree always has this shape.
Why the tree matters
The path of a construct determines its CloudFormation logical ID. CDK joins the path segments, drops the default Resource segment, and appends a short hash. A bucket at DataStack/UploadsBucket gets an ID like UploadsBucket followed by eight hex characters.
This has a practical consequence. If you rename a construct id or move it to a different parent, the logical ID changes. CloudFormation then sees a new resource, creates it, and deletes the old one. For a stateful resource such as a bucket or table, that can mean data loss or a failed deploy.
Stacks as deployment boundaries
Put resources in the same stack when they change and deploy together. Split them when lifecycles differ, for example a database that rarely changes and an application that deploys daily. Keep in mind that a stack has a limit of 500 resources and that one failed resource rolls back its whole stack.
When you run cdk deploy, CDK synthesizes the app first. Each stack becomes a template in the cdk.out directory. With several stacks, name the one you want: cdk deploy DataStack.
Common errors
- Duplicate ids. Two children with the same id under one parent throw
There is already a Construct with name .... - Wrong scope. Passing
appinstead ofthisinside a stack puts the resource outside the stack, and synthesis fails. - Casual renames. Refactoring ids on deployed stateful resources, as described above.
Exercise
You rename the id of a deployed S3 bucket construct from 'UploadsBucket' to 'Uploads', without changing anything else. What does CloudFormation see, and why?
Show answer
The construct path changes, so the logical ID changes. CloudFormation treats it as a new resource to create and the old one as a resource to delete. With the default removal policy and a non-retained bucket, this can destroy the old bucket or cause a name conflict.