You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
The AWS Cloud Development Kit (CDK) is an open-source framework that lets you define cloud infrastructure using familiar programming languages. Instead of writing YAML or JSON templates, you use TypeScript, Python, Java, C#, or Go to define your resources. Under the hood, the CDK generates CloudFormation templates, so you get all the benefits of CloudFormation — dependency resolution, rollback, drift detection — combined with the full power of a programming language.
CloudFormation templates are powerful but have limitations:
The CDK addresses all of these by letting you use a real programming language.
The CDK workflow has three layers:
Your CDK code is a regular application written in TypeScript, Python, or another supported language. It defines one or more stacks.
When you run cdk synth, the CDK compiles your code into a CloudFormation template (called a cloud assembly). This is the "translation" step.
When you run cdk deploy, the CDK submits the synthesised CloudFormation template to AWS, which creates or updates the stack.
CDK Code (TypeScript) → cdk synth → CloudFormation Template (YAML) → cdk deploy → AWS Resources
Constructs are the basic building blocks of a CDK app. Every resource, group of resources, or abstraction is a construct.
There are three levels of constructs:
| Level | Name | Description |
|---|---|---|
| L1 | CFN Resources | Direct 1:1 mapping to CloudFormation resources (e.g., CfnBucket) |
| L2 | Curated Constructs | Higher-level abstractions with sensible defaults (e.g., Bucket) |
| L3 | Patterns | Opinionated combinations of multiple resources (e.g., ApplicationLoadBalancedFargateService) |
A stack in CDK corresponds to a CloudFormation stack. You define a stack as a class:
import * as cdk from 'aws-cdk-lib';
import * as s3 from 'aws-cdk-lib/aws-s3';
export class MyAppStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
new s3.Bucket(this, 'MyBucket', {
versioned: true,
removalPolicy: cdk.RemovalPolicy.DESTROY,
autoDeleteObjects: true,
});
}
}
An app is the root construct. It can contain multiple stacks:
const app = new cdk.App();
new MyAppStack(app, 'DevStack', {
env: { region: 'eu-west-2' },
});
new MyAppStack(app, 'ProdStack', {
env: { region: 'us-east-1' },
});
app.synth();
npm install -g aws-cdk
cdk --version
Before you can deploy CDK apps, you must bootstrap your AWS account and region. This creates an S3 bucket and IAM roles that the CDK uses:
cdk bootstrap aws://123456789012/eu-west-2
mkdir my-cdk-app && cd my-cdk-app
cdk init app --language typescript
This generates a project structure with:
lib/ — your stack definitionsbin/ — the app entry pointtest/ — unit testscdk.json — CDK configurationHere is the same infrastructure defined in both CDK (TypeScript) and CloudFormation YAML:
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.