What you'll learn
- A CDK program is a tree: an App contains Stacks, and stacks contain constructs that stand for AWS resources.
cdk synthruns your program and writes CloudFormation tocdk.out. Nothing is deployed until you runcdk deploy.- The second argument of every construct is its id. It is part of the resource's identity, so choose it once and keep it.
Section 01
Infrastructure as code
Infrastructure as code means the description of your cloud resources lives in files, in version control, next to the application. A pull request can change a table's billing mode, a teammate can review it, and the same files can create an identical copy of the whole backend in another account or Region.
CDK is one way to do that. Instead of writing CloudFormation YAML by hand, you write a program in a language you already use. CDK supports TypeScript, JavaScript, Python, Java, C# and Go; this course teaches TypeScript and Python, and you can switch between them at any time. The concepts are identical.
Section 02
App, stack, construct
A CDK program creates one App, the root. Inside it you create one or more Stacks; each stack becomes one CloudFormation stack, the unit AWS deploys and deletes together. Inside a stack you create constructs: a bucket, a table, a function, or a group of them.
Every construct takes the same first two arguments: its scope (the parent that contains it) and its id (a name, unique among its siblings). Those two arguments place the construct in the tree. Everything after them is configuration, called props.
| Concept | What it is | Becomes |
|---|---|---|
| App | The root of the tree. One per program. | The cloud assembly in cdk.out |
| Stack | A unit of deployment. Its id is the stack name. | One CloudFormation stack |
| Construct | A building block with a scope and an id. | One or more resources |
import * as cdk from "aws-cdk-lib";
import { Construct } from "constructs";
export class PokedexStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// Constructs go here, with `this` as their scope.
}
}
const app = new cdk.App();
new PokedexStack(app, "PokedexStack", {
description: "Backend for the Unova Pokédex",
});Section 03
The CDK workflow
You will use only a handful of CDK commands. These are the ones that matter, in the order you use them:
Everything in this course uses cdk synth: when you press “Check solution” a simulator reads your code, builds the construct tree, and shows you the CloudFormation it would synthesize. No AWS account, no credentials, nothing is deployed.
npm install -g aws-cdk # the CLI (separate from the aws-cdk-lib library)
cdk init app --language typescript # or: --language python
cdk synth # run the program, write CloudFormation to cdk.out/
cdk diff # compare with what is deployed right now
cdk deploy # synth, then ask CloudFormation to create or update
cdk destroy # delete the stack and what it ownsSection 04
Why the id matters
CDK turns the path from the root to a construct into its CloudFormation logical ID: a stack named PokedexStack with a bucket CardsBucket gets a logical ID like CardsBucket685ECB20. If you later rename the id, the logical ID changes, and CloudFormation sees a brand-new resource: it creates a new, empty one and then removes the old one from the stack. With the default RETAIN the old one is orphaned, but with DESTROY it is deleted, data included. Either way, your app now points at an empty resource. Choose ids carefully and treat them as permanent.
Try it · The construct tree
Pick a node. The path from the stack down is what CDK hashes into the logical ID.
construct
CardsBucket
An L2 construct: s3.Bucket. It owns the resource underneath, and can add more (a policy, grants).
- Path
- PokedexStack/CardsBucket
Rename CardsBucket in your code and its path, hash and logical ID all change: CloudFormation then creates a new bucket and deletes the old one.
Practice
Your turn
The PokedexStack class exists, but nothing creates it, so cdk synth would produce an empty cloud assembly. Instantiate it once in the app with the id PokedexStack, a description, and an env that pins it to the ap-south-1 Region.
new PokedexStack(app, "PokedexStack", { description, env: { region: "ap-south-1" } }).The simulator runs the constructs this course teaches, plus simple loops and helper methods. Other CDK code may not be recognised.
Check
Check your understanding
Quick Quiz · one question, as many tries as you need.
cdk synth on the Pokédex app. What happens?Choose one answer.