AWS CDK for operations teams
-
Phase 0: Before you start
-
Phase 1: The foundation
-
Phase 2: Operations and security
-
Phase 3: SQL Server on AWS
-
Phase 4: Troubleshooting step by step
-
Phase 5: Final project and career
A 400-line CloudFormation template is hard to read and easy to get wrong, the IAM parts most of all. This AWS CDK tutorial takes the same idea into a real programming language, where a few lines of TypeScript produce a complete CloudFormation template you can review, with least-privilege IAM written for you.
You’ll install the CDK, bootstrap your account and build a stack with a bucket, a security group in saat-vpc, an app role that can read the bucket and an SSM parameter. Then you’ll diff, deploy, change and destroy it, and along the way fix a failed VPC lookup and a bucket that survives cdk destroy.
| Time | You need | By the end you can |
|---|---|---|
| 60 minutes | You need saat-vpc, your laptop signed in as saat-admin, winget (Windows 10 or 11) and about 1 GB of disk for Node.js and packages. The CDK bootstrap bucket costs a few cents and the demo stack itself costs pennies. | Install Node.js and the CDK CLI, bootstrap an environment, and write a TypeScript stack with constructs, grants and lookups. Use cdk synth, diff, deploy and destroy, and deal with lookup errors and retained resources. |
What it is, in one line
The AWS CDK (Cloud Development Kit) lets you define AWS infrastructure in code such as TypeScript or Python, and turns that code into CloudFormation templates it deploys for you.
In Simple Words
Ordering a thali instead of each dish
At a restaurant you could order rice, dal, two sabzis, roti, curd, pickle and a sweet one at a time, telling the waiter how much of each. Or you could order a thali, get a sensible set from the kitchen and only ask for changes like “no sweet, extra roti”.
Writing a CloudFormation template is ordering every dish yourself, and a CDK construct is the thali: new s3.Bucket() comes with sensible settings and you only mention what’s different.
The bill still comes from the same kitchen, though. What the CDK produces is a normal CloudFormation template, so everything from Lesson 34 still applies.
Engineer's View
How the CDK works
A CDK app holds stacks, and stacks hold constructs. L1 constructs (CfnBucket) map one-to-one to CloudFormation resources. L2 constructs (s3.Bucket) add defaults and helper methods such as grantRead(), which writes a least-privilege IAM policy for you, and L3 constructs (patterns) build several resources together.
cdk synth runs your code and writes CloudFormation templates into cdk.out, and cdk diff compares them with the deployed stack, showing IAM and security group changes in a separate view. cdk deploy uploads assets and creates a change set. Each account and Region has to be bootstrapped before its first deploy, which means the CDKToolkit stack creates an S3 bucket, an ECR repository and the roles the CDK uses.
Lookups like Vpc.fromLookup query your account at synth time and cache what they find in cdk.context.json, which you commit so synth gives the same result every time. Watch the removal policies too: buckets and other stateful resources default to RETAIN, so cdk destroy leaves them behind unless you say otherwise. The CDK CLI also has its own version line (2.1000 and above since 2025), separate from the library.
How it works, step by step
This is what happens when you run cdk deploy.
1Your code describes resources
lib/saat-cdk-stack.ts creates objects such as a bucket, a security group and a role. It’s ordinary code, so loops, conditions and functions all work.
2Synth writes the template
The CDK turns those objects into a CloudFormation template and adds the IAM policy for grantRead and a bucket policy for enforceSSL on the way.
3Diff and deploy use CloudFormation
A change set gets created and executed, with rollback on failure, the same way as in Lesson 34.
4Why ops teams use it
What makes it worth it isn’t the language so much as what the constructs write for you:
On the other side, you need Node.js (or Python), and you have to review the synthesized template and not only the code. Running cdk diff every time is the habit that keeps it safe.
✅ Checkpoint: You can explain constructs, synth, diff, deploy and bootstrap, and why lookups and removal policies need your attention.
Hands-on lab: an AWS CDK tutorial in TypeScript
Before you run any command, double-check it
Read every command twice before you press Enter. Check the target: the system or account, the IDs, the names and the sizes. These commands are for learning and were tested in our lab, not on your system. At work, run the read-only list commands first, follow your change process, make sure a backup exists, and ask a second person to check anything that changes or deletes data. You run them at your own risk.
Install Node.js and the CDK CLI. After the first line, close and reopen PowerShell so it picks up the new PATH:
winget install OpenJS.NodeJS.LTS
npm install -g aws-cdk
node --version
cdk --versionYou should see something like this:
v22.20.0
2.1031.0 (build 3c4d5e6)1Give the CDK credentials and bootstrap
The CDK reads standard AWS credentials, and the easiest way to give it some is to export the temporary credentials from your aws login session. They only last about 15 minutes, so if you see ExpiredToken later, run these three lines again.
Paste this whole block:
cd C:\aws-lab
aws login --profile saat-admin
aws configure export-credentials --profile saat-admin --format powershell | Invoke-Expression
Remove-Item Env:AWS_PROFILE -ErrorAction SilentlyContinue
$v = Get-Content C:\aws-lab\saat-vpc.json | ConvertFrom-Json
$acct = aws sts get-caller-identity --query Account --output text
cdk bootstrap "aws://$($acct)/ap-south-1" --tags course=saatvedha-awsYou should see something like this (the first time takes two or three minutes):
⏳ Bootstrapping environment aws://111122223333/ap-south-1...
✅ Environment aws://111122223333/ap-south-1 bootstrapped.You only bootstrap once per account and Region. It creates the CDKToolkit stack, which you should keep for later lessons.
2Create the app and write the stack
Paste this whole block:
mkdir C:\aws-lab\saat-cdk | Out-Null
cd C:\aws-lab\saat-cdk
cdk init app --language typescript | Select-Object -Last 3
@'
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as s3 from 'aws-cdk-lib/aws-s3';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as iam from 'aws-cdk-lib/aws-iam';
import * as ssm from 'aws-cdk-lib/aws-ssm';
export class SaatCdkStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const vpc = ec2.Vpc.fromLookup(this, 'SaatVpc', { vpcId: this.node.tryGetContext('vpcId') });
const logs = new s3.Bucket(this, 'Logs', {
encryption: s3.BucketEncryption.S3_MANAGED,
blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL,
enforceSSL: true,
lifecycleRules: [{ expiration: cdk.Duration.days(30) }],
});
const sg = new ec2.SecurityGroup(this, 'WebSg', { vpc, description: 'Web tier from CDK' });
sg.addIngressRule(ec2.Peer.anyIpv4(), ec2.Port.tcp(443), 'HTTPS from anywhere');
const appRole = new iam.Role(this, 'AppRole', { assumedBy: new iam.ServicePrincipal('ec2.amazonaws.com') });
logs.grantRead(appRole);
new ssm.StringParameter(this, 'LogBucketParam', { parameterName: '/saat/cdk/log-bucket', stringValue: logs.bucketName });
cdk.Tags.of(this).add('course', 'saatvedha-aws');
new cdk.CfnOutput(this, 'LogBucketName', { value: logs.bucketName });
}
}
'@ | Set-Content lib\saat-cdk-stack.ts
@'
#!/usr/bin/env node
import * as cdk from 'aws-cdk-lib';
import { SaatCdkStack } from '../lib/saat-cdk-stack';
const app = new cdk.App();
new SaatCdkStack(app, 'SaatCdkStack', {
env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: 'ap-south-1' },
});
'@ | Set-Content bin\saat-cdk.ts
cdk synth -c vpcId=$($v.VpcId) | Measure-Object -Line | Select-Object -ExpandProperty Lines
Get-Content cdk.context.json | Select-Object -First 3You should see something like this:
✅ All done!
...
212
{
"vpc-provider:account=111122223333:filter.vpc-id=vpc-0a1b2c3d4e5f67890:region=ap-south-1:returnAsymmetricSubnets=true": {
"vpcId": "vpc-0a1b2c3d4e5f67890",About 25 lines of TypeScript turned into a template of more than 200 lines, including the bucket policy that enforces TLS and the IAM policy for grantRead. The VPC lookup result is cached in cdk.context.json, and teams commit that file.
3Diff and deploy
Paste this, read through the security changes it shows you, and then answer y:
cdk deploy -c vpcId=$($v.VpcId)You should see something like this (after the IAM and security group change tables):
Do you wish to deploy these changes (y/n)? y
SaatCdkStack: deploying... [1/1]
SaatCdkStack: creating CloudFormation changeset...
✅ SaatCdkStack
Outputs:
SaatCdkStack.LogBucketName = saatcdkstack-logs1a2b3c4d-9z8y7x6w5v4uYou get the approval prompt whenever IAM or security groups change, and in a pipeline you’d review the same details in the pull request. If you open CloudFormation you’ll find SaatCdkStack sitting there like any other stack.


4Change the code and see the diff
Raise retention to 60 days, run a diff and deploy:
(Get-Content lib\saat-cdk-stack.ts -Raw) -replace 'Duration.days\(30\)', 'Duration.days(60)' | Set-Content lib\saat-cdk-stack.ts
cdk diff -c vpcId=$($v.VpcId)
cdk deploy -c vpcId=$($v.VpcId) --require-approval neverYou should see something like this:
Stack SaatCdkStack
Resources
[~] AWS::S3::Bucket Logs Logs6819BB44
└─ [~] LifecycleConfiguration
└─ [~] .Rules:
- "ExpirationInDays": 30
+ "ExpirationInDays": 60
✅ SaatCdkStackThe diff shows a single property on a single resource and no replacement. Using --require-approval never is fine here because no IAM changed, but keep the prompt (the default) for anything that touches permissions.
✅ Checkpoint: The CDK is installed, ap-south-1 is bootstrapped, SaatCdkStack is deployed with a bucket, security group, role and parameter, and a diff showed you the 30 to 60 day change before you deployed it. Keep the stack for the break lab.
Break It, Then Fix It
Here are two CDK surprises. One stops you at synth and the other leaves something behind after destroy.
1Break it: look up a VPC that doesn’t exist
Run synth with a made-up VPC ID:
cdk synth -c vpcId=vpc-0123456789abcdef0You’ll get an error like this:
[Error at /SaatCdkStack] Could not find any VPCs matching {"account":"111122223333","region":"ap-south-1","filter":{"vpc-id":"vpc-0123456789abcdef0"},"returnAsymmetricSubnets":true,"lookupRoleArn":"arn:aws:iam::111122223333:role/cdk-hnb659fds-lookup-role-111122223333-ap-south-1"}Lookups run against your real account during synth, so a wrong ID, the wrong Region or credentials for the wrong account all land you here. The opposite problem is quieter, because once a lookup succeeds its result is cached and synth keeps using the old answer even after the VPC changes. Fix: pass the right ID, and clear stale lookups whenever the infrastructure changes:
cdk context --clear
cdk synth -c vpcId=$($v.VpcId) | Out-Null
"synth ok"You should see this:
synth ok2Break it: destroy the stack and check for leftovers
Destroy the stack and then look for the bucket:
$bucketName = aws cloudformation describe-stacks --stack-name SaatCdkStack --query "Stacks[0].Outputs[?OutputKey=='LogBucketName'].OutputValue" --output text
cdk destroy -c vpcId=$($v.VpcId) --force
aws s3api head-bucket --bucket $bucketName
"exit code: $LASTEXITCODE"You should see something like this:
SaatCdkStack: destroying... [1/1]
✅ SaatCdkStack: destroyed
{
"BucketRegion": "ap-south-1",
"AccessPointAlias": false
}
exit code: 0
The stack has gone but the bucket is still there. The CDK sets RETAIN on buckets by default so you don’t lose data by accident, which is right for production, but in dev accounts it leaves behind hundreds of orphaned buckets that nobody owns.
Fix: delete the leftovers, and for non-production stacks spell out what you want, for example removalPolicy: cdk.RemovalPolicy.DESTROY, autoDeleteObjects: true:
aws s3 rb "s3://$bucketName" --force
cd C:\aws-lab
$env:AWS_PROFILE = 'saat-admin'
Remove-Item Env:AWS_ACCESS_KEY_ID, Env:AWS_SECRET_ACCESS_KEY, Env:AWS_SESSION_TOKEN -ErrorAction SilentlyContinue
.\Find-LabResources.ps1Keep the C:\aws-lab\saat-cdk folder if you want to experiment further, and keep the CDKToolkit stack.
✅ Checkpoint: You fixed a failed VPC lookup and cleared the context cache, then found and removed a bucket that cdk destroy had retained. SaatCdkStack is gone and the CDKToolkit bootstrap stack stays.
This happened at work
The 1,900 buckets nobody owned
A software company let every feature branch deploy its own CDK stack into a shared dev account, and the stack was destroyed once the branch merged. Nobody looked closely for two years, until a cost review turned up 1,900 S3 buckets and 600 log groups in that account, costing more each month than production storage did.
Every one of those buckets had been retained by default when its stack was destroyed. Some held test data copied from production, customer names included, so what started as a cost problem became a data protection problem.
The fix was a CDK Aspect that sets DESTROY with autoDeleteObjects on every bucket in dev stacks and fails synth when a production stack has no explicit removal policy, along with a weekly report of buckets that no longer have a live owning stack.
Common mistakes and troubleshooting
| Mistake | What happens | What to do |
|---|---|---|
| Assuming cdk destroy removes everything | Buckets and log groups are retained | Set removal policies explicitly; check for leftovers |
| Not committing cdk.context.json | Different results on different machines | Commit it; clear it deliberately when infra changes |
| Skipping cdk diff | Surprise replacements or IAM changes | Diff every change; keep the approval prompt for IAM |
| Deploying to an unbootstrapped Region | Error asking you to run cdk bootstrap | Bootstrap each account and Region once |
| Writing IAM policies by hand in CDK | Over-broad permissions | Use grant methods like grantRead |
| Long-lived access keys for the CDK | Leaked keys | Use temporary credentials (aws login, SSO or pipeline roles) |
Interview questions
Click a question to open it. Each one is phrased the way an interviewer would ask it, and underneath you’ll find why they ask it and one way you could answer in your own words.
Fresher level
1. Can you explain in simple words what the AWS CDK is?
Why they ask this: They want to know you understand how the CDK relates to CloudFormation. Plenty of people think it replaces CloudFormation, when in fact it runs on top of it.
How you might answer: The CDK lets you define AWS infrastructure in a programming language like TypeScript or Python. When you run it, it generates a CloudFormation template and deploys that through CloudFormation.
So I still get stacks, change sets and rollback, I’m just writing code instead of long YAML.
2. You try your first cdk deploy in a new account and it fails. Someone tells you to run cdk bootstrap. What does that actually do?
Why they ask this: They’re checking you know the setup the CDK needs in each account, because it’s the first thing that trips people up in a new account or Region.
How you might answer: cdk bootstrap deploys the CDKToolkit stack into that account and Region, which creates an S3 bucket and an ECR repository for assets plus the IAM roles the CDK deploys with.
It has to be run once per account and Region before the first deploy.
3. What’s the difference between cdk synth and cdk deploy?
Why they ask this: They want to know you understand the CDK workflow and whether you check your work before touching a real account.
How you might answer: cdk synth only generates the CloudFormation template on my laptop and nothing changes in AWS. cdk deploy synthesizes as well, but then it uploads assets and creates and executes a change set in my account.
When I want to read the template before deploying, I run synth first.
2–5 years experience
4. How would you explain L1, L2 and L3 constructs to a new team member?
Why they ask this: They want to see you understand the CDK’s building blocks, because the level you pick decides how much code you write and how safe the defaults are.
How you might answer: I’d tell them an L1 construct maps straight to one CloudFormation resource with nothing extra, and an L2 adds sensible defaults and helper methods, like grants for permissions.
An L3 is a pattern that puts several resources together into a common architecture, so a whole setup takes a few lines.
5. You ran cdk destroy on a test stack, but the S3 bucket is still there. Why did that happen?
Why they ask this: They’re checking you know the CDK protects data by default, and that you understand when it’s safe to change that.
How you might answer: Stateful resources like buckets default to RETAIN, so the CDK leaves them behind to protect the data. On a dev stack where deleting is safe, I’d set removalPolicy to DESTROY, plus autoDeleteObjects for buckets so they get emptied first.
6. A teammate opens a pull request that changes a CDK app. How would you review it safely?
Why they ask this: They’re looking for good change control, since a small code change in the CDK can turn into a big infrastructure change.
How you might answer: I’d run cdk diff in the pull request to see what will change in the account, read the synthesized template, and look hard at the security diff for IAM and security group changes.
After that the change would go out through a pipeline with an approval step before production.
Senior level
7. Your team is choosing between CDK, plain CloudFormation and Terraform. How would you help them decide?
Why they ask this: They want balanced judgement about tools, and whether you choose based on the team and the work rather than your own taste.
How you might answer: The CDK suits teams who are comfortable with code and want abstractions, grants and testing on AWS. Plain CloudFormation is fine for small templates that rarely change, and Terraform makes sense for multi-cloud or for teams already standardized on it.
Whichever we choose, the important part is sticking to one tool, reviewing every change and stopping changes in the console.
8. You have 50 CDK apps owned by different teams. How would you make sure they all follow the same security standards?
Why they ask this: They want to see governance at scale, where the standards are built into the tools so teams don’t have to remember them.
How you might answer: I’d publish shared construct libraries with secure defaults so teams get encryption and tags without extra effort, and use Aspects to check or fix every construct for encryption, removal policies and tags.
CI would run cdk-nag rules, and every pipeline would include diff review and approvals.
Quick quiz
Q1. What does the CDK produce before deploying?
- A shell script
- A CloudFormation template
- A Terraform plan
- An AMI
Show answer
B — cdk synth writes CloudFormation templates.
Q2. What must exist in an account and Region before the first cdk deploy?
- A VPC
- The bootstrap stack (CDKToolkit)
- An EC2 instance
- A KMS key
Show answer
B — cdk bootstrap creates it.
Q3. What does bucket.grantRead(role) do?
- Makes the bucket public
- Adds a least-privilege read policy for the role
- Deletes the bucket
- Enables versioning
Show answer
B — Grants write the IAM policy for you.
Q4. Why did the bucket survive cdk destroy?
- A bug
- Buckets default to RemovalPolicy RETAIN
- The stack failed
- Versioning was on
Show answer
B — Stateful resources are retained by default.
Q5. Where are lookup results cached?
- cdk.out
- cdk.context.json
- package.json
- S3
Show answer
B — Commit it, and clear it when infrastructure changes.
Quick recap
- The CDK turns code into CloudFormation; change sets, rollback and drift still apply.
- Bootstrap once per account and Region.
- L2 constructs give secure defaults and grant methods for least privilege.
- Run cdk diff before every deploy; read the security changes.
- Lookups are cached in cdk.context.json; stateful resources default to RETAIN.
Next lesson
Lesson 36: Cost management: Budgets, Cost Explorer, Savings Plans → Next you’ll set budgets with alerts, find what’s driving your bill in Cost Explorer and see how Savings Plans and rightsizing bring costs down.
Further reading: AWS CDK tutorial
Want to go deeper into AWS CDK tutorial? Read What is the AWS CDK? and AWS CDK bootstrapping. You can also see the full plan for this course on the AWS Cloud Administration course page.
