Skip to content

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-lib and constructs in package.json, plus a cdk.json
  • A bin/*.ts file calling new App()
  • Classes extending Stack or Construct under lib/

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

  1. bin/app.ts, to see which stacks exist and with what props
  2. The stack that owns the resource you care about
  3. grant* calls, which define permissions

How a request moves through the code

  1. 1cdk synth runs bin/app.ts.
  2. 2Each stack's constructor creates constructs, passing references between them.
  3. 3grantRead, grantWrite and similar calls generate IAM policies.
  4. 4The 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.