How to understand a AWS CDK codebase
The AWS CDK defines cloud infrastructure as TypeScript classes. A CDK app instantiates stacks, and stacks contain constructs (buckets, functions, queues). Running `cdk synth` turns the code into CloudFormation. To read it, you follow resources and the permissions granted between them.
How to tell it's AWS CDK
aws-cdk-libandconstructsinpackage.json, plus acdk.json- A
bin/*.tsfile callingnew App() - Classes extending
StackorConstructunderlib/
Where things usually live
- bin/app.ts
- Creates the app and each stack, often per environment.
- lib/*-stack.ts
- Stacks that declare resources.
- lib/constructs/
- Reusable custom constructs.
- lambda/ or src/
- Function code bundled by
NodejsFunction. - cdk.json
- The app command and context values.
Conventions, not rules. Real projects rename and move things, so confirm against the repository.
Where to start reading
bin/app.ts, to see which stacks exist and with what props- The stack that owns the resource you care about
grant*calls, which define permissions
How a request moves through the code
cdk synthrunsbin/app.ts.- Each stack's constructor creates constructs, passing references between them.
grantRead,grantWriteand similar calls generate IAM policies.- The CLI writes CloudFormation templates and deploys them.
Questions to ask about the repository
- List the stacks and the main resources in each.
- Which functions can read or write each table or bucket?
- How do the stacks differ between environments?
What trips people up
- Changing a construct's ID replaces the resource, which can delete data.
- Cross-stack references create CloudFormation exports that block deleting or changing the producer stack.