What Is AWS CDK? Cloud Development Kit in TypeScript and Python Explained
CloudFormation YAML is like writing a novel in pure HTML. AWS CDK lets you write TypeScript or Python that *generates* that CloudFormation — loops, types, and your editor help you, then synth emits the template.
In this chapter
- Why CDK exists beside raw CloudFormation
- App, Stack, Construct levels
- cdk synth, diff, deploy, destroy
- CDK vs Terraform vs raw YAML
- Lab, errors, FAQ, interview
1. Everyday example first
A Mumbai team copied 800-line YAML for every microservice. One parameter typo broke prod. CDK constructs gave them a typed Factory for "API + Lambda + alarm".
AWS CDK (Cloud Development Kit) is a framework: you write constructs in TypeScript, Python, Java, Go or C#. cdk synth emits CloudFormation; cdk deploy ships it. L2 constructs add sane defaults; L3 patterns bundle architectures.
2. App, Stack, Construct
Think of one CDK app for the college’s document upload service. A construct is a building block in its code. A bucket construct represents the bucket, and a pattern construct can include several related resources. A stack groups the resources that CloudFormation will deploy together. The app can contain a test stack and a production stack. Follow the bucket in code to the resource type AWS::S3::Bucket in the generated template, then to the deployed bucket after a successful change.
| Level | Meaning | Example |
|---|---|---|
| App | Root; holds stacks | bin/app.ts |
| Stack | One deployable CFN unit | StagingApiStack |
| Construct L1 | 1:1 CFN resource | CfnBucket |
| Construct L2 | Curated helper | s3.Bucket |
| Construct L3 | Pattern / kit | api-gateway + lambda kit |
# Python sketch
from aws_cdk import App, Stack, RemovalPolicy
from aws_cdk import aws_s3 as s3
class LogsStack(Stack):
def __init__(self, scope, id, **kw):
super().__init__(scope, id, **kw)
s3.Bucket(self, "Logs",
versioned=True,
removal_policy=RemovalPolicy.DESTROY,
auto_delete_objects=True)
app = App()
LogsStack(app, "LabLogsStack")
app.synth()
3. CDK vs Terraform vs YAML
CDK code runs during synthesis to build the CloudFormation template. CloudFormation then handles creating or updating the deployed resources. Read the generated output before deploying so you can answer which resource the code asks AWS to create and which settings it gives that resource. A reusable construct lets you share good settings for the next stack. That brings consistency only if its generated resource properties are the ones you intended.
- Raw YAML/JSON: universal, verbose, weak editor help.
- CDK: best when your team already ships TypeScript/Python and lives on AWS.
- Terraform: strong multi-cloud and existing module culture.
- Many orgs use CDK inside AWS accounts and Terraform at the org edge — both fine.
Ravindra Bagale's Tip
Read every cdk diff like a production change ticket. Synth is not deploy.
4. Industries
- Product startups: L3 patterns for each microservice.
- Enterprises: CDK Pipelines + CodePipeline for git-driven deploys.
- Data platforms: typed Glue/S3/Lake constructs.
- Security teams: constructs that force KMS + logging.
- Agencies: publish private construct libraries per client baseline.
5. Mini lab: bucket stack
Start by synthesising the lab stack and find the single bucket resource in the generated template. Read its versioning and removal behavior. If you change the bucket property, repeat synthesis and review the difference. In a sandbox deployment, verify the stack status and the actual bucket setting. During cleanup, confirm which resources belonged to your lab stack and keep any shared bootstrap assets.
Do this in ap-south-1 (Mumbai) unless your org forbids it. Set a budget alert first.
- Install Node LTS + aws-cdk CLI; aws configure sandbox.
- cdk init app --language typescript (or python).
- Add an s3.Bucket with removal_policy DESTROY for lab.
- cdk synth && cdk diff && cdk deploy.
- Open CloudFormation console; find the nested template resources.
Cleanup (stop billing)
- cdk destroy — confirm bucket empty/auto-delete.
- Delete leftover CDKToolkit bootstrap stack only if your org allows and nothing else uses it.
6. Common errors and fixes
Each row is a real mistake students and juniors hit. Read the fix before you panic.
| Error / symptom | Fix |
|---|---|
| Need to perform AWS calls / bootstrap | Run cdk bootstrap in the account/region once. |
| Checksum / asset failures | Clean cdk.out; check Docker if bundling Lambdas. |
| Hotswap false confidence | Hotswap skips full CFN — not for prod IAM changes. |
| Stack stuck DELETE_FAILED | Empty S3/non-empty buckets; set auto_delete or empty manually. |
| Context growing huge | Prune cdk.context.json carefully in teams. |
| Circular dependency | Split stacks or use lazy values / exports wisely. |
| Wrong account from SSO | Check aws sts get-caller-identity before deploy. |
| IAM too open in L2 defaults | Review generated policies; tighten. |
| Python venv missing cdk libs | pip install -r requirements.txt inside venv. |
| Diff looks empty but expect change | You edited wrong stack id / env. |
7. Troubleshooting checklist
- aws sts get-caller-identity
- cdk doctor
- rm -rf cdk.out && cdk synth
- Read CloudFormation events for the failed resource.
- Compare with a minimal empty stack deploy.
8. FAQ
Q. Does CDK replace CloudFormation?
No — it generates and deploys it.
Q. TypeScript or Python?
Pick the language your team already reviews in PRs.
Q. Can I escape to raw CFN?
Yes — L1 Cfn* resources and escapes.
Q. Is cdk deploy recursive for all stacks?
You choose stacks; use stages in pipelines for order.
Q. Unit testing?
assertions module / template hasResource — test synth output.
9. Interview Q&A
Q. What is a construct?
A reusable building block that emits one or more CFN resources.
Q. synth vs deploy?
synth only writes templates; deploy applies them to AWS.
Q. L1 vs L2?
L1 maps 1:1 to CFN; L2 adds defaults and helper methods.
Q. Why bootstrap?
Account/region needs CDK toolkit assets bucket and roles.
Q. CDK vs Terraform?
CDK = AWS-first code in familiar languages; TF = HCL multi-cloud.
Q. How to avoid snowflake stacks?
Shared construct libraries + pipelines.
10. Tips in English, Marathi and Hindi
Ravindra Bagale's Tip (EN)
If you cannot read the synth diff, you cannot own the outage.
Ravindra Bagale's Tip (MR)
synth diff वाचता येत नसेल तर outage own करता येणार नाही.
Tip (HI)
synth diff नहीं पढ़ सकते तो outage की ज़िम्मेदारी नहीं ले सकते।
Try at home
- Init a CDK app and deploy one bucket to sandbox.
- Open cdk.out and find the generated template.
- List three YAML stanzas you would wrap as L3.
In short
- CDK writes CloudFormation using real programming languages.
- App → Stack → Constructs; synth then deploy.
- Bootstrap once per account/region.
- Review diffs; destroy lab stacks.
Revision, interview and comparison cards
Revise this lesson in a few minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens this lesson.











More cards: Ravi Sir's Cards.
Practise this chapter
Interview questions and answers
How do you show that a line in a CDK app really changes an AWS property?
Synthesize the stack and find the corresponding CloudFormation resource and property. Review the difference and, in the sandbox, compare the actual deployed setting afterward. The construct code describes behavior and CloudFormation applies the generated infrastructure definitions.
Check your understanding
Choose an answer, then compare your reasoning with the explanation.
Practice Tasks
Practice checks run in your browser. They check the answer for this exercise; they do not create resources or connect to your AWS account.
About this scenarioAmravati Agro Market is a agriculture business in Amravati. Anjali Thakre looks after its IT and cloud work. They count money in INR and run their AWS resources in the ap-south-1 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.
0 / 16
Easy cdk.json app entry
CDK needs an app command.
cdk.json with app npx ts-node bin/app.ts.
- Start from the top-level keys the task names.
- Indent child keys with 2 spaces (no tabs).
- Fill in each property with the value from the task.
- Click Check answer. Key order does not matter.
Easy List CloudFormation stacks
CDK synthesizes to CFN.
List CloudFormation stacks in ap-south-1.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Easy Describe a stack
See outputs after cdk deploy.
Describe stack amravatiagro-AppStack.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Easy Delete a stack
Tear down lab stacks.
Delete stack amravatiagro-AppStack.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Easy List buckets CDK created
Verify Bucket construct.
List all S3 buckets.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Easy CDK bootstrap stack count typical
cdk bootstrap creates CDKToolkit.
How many primary bootstrap stacks named CDKToolkit does a region usually get?
- Write down the numbers the task gives you.
- Work it out step by step on paper.
- Type only the final answer in the box.
- Click Check answer.
Easy CDK v2 major version number
Modern apps use v2.
What major version number is AWS CDK v2?
- Write down the numbers the task gives you.
- Work it out step by step on paper.
- Type only the final answer in the box.
- Click Check answer.
Medium cdk.context.json AZ list
Context caches lookups.
JSON with availability-zones key mapping ap-south-1 to [ap-south-1a, ap-south-1b].
- Start from the top-level keys the task names.
- Indent child keys with 2 spaces (no tabs).
- Fill in each property with the value from the task.
- Click Check answer. Key order does not matter.
Medium Stack events
Debug failed deploys.
Describe stack events for amravatiagro-AppStack.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Medium List stack resources
Map constructs to physical ids.
List stack resources for amravatiagro-AppStack.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Medium Pipeline stages example
Dev then Prod is two.
If a pipeline deploys Dev then Prod, how many app stages is that?
- Write down the numbers the task gives you.
- Work it out step by step on paper.
- Type only the final answer in the box.
- Click Check answer.
Medium CloudFormation Outputs snippet
Expose bucket name.
JSON Outputs with BucketName Value Ref DataBucket.
- Start from the top-level keys the task names.
- Indent child keys with 2 spaces (no tabs).
- Fill in each property with the value from the task.
- Click Check answer. Key order does not matter.
Medium Stack tag Project
Tags flow to resources.
JSON Tags array one Key Project Value learn.
- Start from the top-level keys the task names.
- Indent child keys with 2 spaces (no tabs).
- Fill in each property with the value from the task.
- Click Check answer. Key order does not matter.
Medium Get a role
CDK creates roles too.
Get IAM role amravatiagro-AppRole.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Medium Get Lambda function config
Function construct maps here.
Get function configuration for amravatiagro-api.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Hard Allow CFN to manage S3
Deployment role needs rights.
Allow s3:CreateBucket and s3:DeleteBucket on arn:aws:s3:::amravatiagro-* .
- Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
- In each statement write Effect, Action and Resource.
- Give only what the task needs (least privilege).
- Use double quotes and commas correctly. Click Check answer.