AWS CDK Tutorial, for Beginners
AWS CDK Tutorial, for Beginners
1. What is AWS CDK?
AWS Cloud Development Kit (CDK) is a framework that lets you define cloud infrastructure using real programming languages like TypeScript, Python, Java, etc.
Instead of writing long YAML/JSON files (like CloudFormation), you can use logic like loops, conditions, and functions.
2. Prerequisites
- Node.js (v14+)
- AWS CLI installed and configured (
aws configure
) - Basic familiarity with JavaScript/TypeScript
3. Install AWS CDK
npm install -g aws-cdk
cdk --version # should show the installed version
4. Create a New CDK Project
mkdir my-cdk-app
cd my-cdk-app
cdk init app --language typescript
This generates a CDK project with this structure:
my-cdk-app/
āāā bin/
ā āāā my-cdk-app.ts # Entry point
āāā lib/
ā āāā my-cdk-app-stack.ts # Stack definition
āāā cdk.json # CDK settings
āāā package.json
5. Add an AWS S3 Bucket (Example Resource)
Edit lib/my-cdk-app-stack.ts
:
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as s3 from 'aws-cdk-lib/aws-s3';
export class MyCdkAppStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
new s3.Bucket(this, 'MyFirstBucket', {
versioned: true,
removalPolicy: cdk.RemovalPolicy.DESTROY, // cleans up on stack delete
autoDeleteObjects: true, // required for DESTROY policy
});
}
}
6. Build the Project
CDK uses TypeScript, so you must compile it before deploying:
npm run build
7. Bootstrap Your Environment (Only Once Per Account/Region)
cdk bootstrap
This sets up the AWS environment to support CDK deployments (like S3 for storing assets).
8. Deploy the Stack
cdk deploy
CDK will:
- Synthesize the stack (generate CloudFormation template)
- Show you a preview
- Ask for confirmation
- Deploy the resources (in this case, an S3 bucket)
9. Clean Up (Delete Resources)
cdk destroy
Destroys the entire stack and deletes resources like the bucket.
10. Common CDK Commands
Command | Purpose |
---|---|
cdk init | Initialize a CDK project |
cdk synth | Output the CloudFormation template |
cdk deploy | Deploy the stack to AWS |
cdk destroy | Remove all resources in the stack |
cdk diff | Show changes between current vs. deployed stack |
cdk bootstrap | Prepare environment for CDK usage |
Summary
- CDK lets you write infra using code (like TypeScript)
- No need to manage raw YAML or JSON
- CDK apps are compiled and synthesized into CloudFormation templates
Optional: Add More Resources
You can add more AWS services by importing their modules and defining them inside the constructor
, like:
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as apigateway from 'aws-cdk-lib/aws-apigateway';
Let me know if you want a Lambda + API Gateway example next.
This guide fits in about 2.5 pages when printed or exported as PDF. Let me know if you want a ready-to-download PDF or want the same guide in Python. Here is a simple, practical AWS CDK tutorial, limited to ~3 pages. It walks you through creating and deploying a basic AWS resource (an S3 bucket) using AWS CDK in TypeScript.
ā AWS CDK Tutorial (for Beginners)
1. What is AWS CDK?
AWS Cloud Development Kit (CDK) is a framework that lets you define cloud infrastructure using real programming languages like TypeScript, Python, Java, etc.
Instead of writing long YAML/JSON files (like CloudFormation), you can use logic like loops, conditions, and functions.
2. Prerequisites
- Node.js (v14+)
- AWS CLI installed and configured (
aws configure
) - Basic familiarity with JavaScript/TypeScript
3. Install AWS CDK
npm install -g aws-cdk
cdk --version # should show the installed version
4. Create a New CDK Project
mkdir my-cdk-app
cd my-cdk-app
cdk init app --language typescript
This generates a CDK project with this structure:
my-cdk-app/
āāā bin/
ā āāā my-cdk-app.ts # Entry point
āāā lib/
ā āāā my-cdk-app-stack.ts # Stack definition
āāā cdk.json # CDK settings
āāā package.json
5. Add an AWS S3 Bucket (Example Resource)
Edit lib/my-cdk-app-stack.ts
:
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as s3 from 'aws-cdk-lib/aws-s3';
export class MyCdkAppStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
new s3.Bucket(this, 'MyFirstBucket', {
versioned: true,
removalPolicy: cdk.RemovalPolicy.DESTROY, // cleans up on stack delete
autoDeleteObjects: true, // required for DESTROY policy
});
}
}
6. Build the Project
CDK uses TypeScript, so you must compile it before deploying:
npm run build
7. Bootstrap Your Environment (Only Once Per Account/Region)
cdk bootstrap
This sets up the AWS environment to support CDK deployments (like S3 for storing assets).
8. Deploy the Stack
cdk deploy
CDK will:
- Synthesize the stack (generate CloudFormation template)
- Show you a preview
- Ask for confirmation
- Deploy the resources (in this case, an S3 bucket)
9. Clean Up (Delete Resources)
cdk destroy
Destroys the entire stack and deletes resources like the bucket.
10. Common CDK Commands
Command | Purpose |
---|---|
cdk init | Initialize a CDK project |
cdk synth | Output the CloudFormation template |
cdk deploy | Deploy the stack to AWS |
cdk destroy | Remove all resources in the stack |
cdk diff | Show changes between current vs. deployed stack |
cdk bootstrap | Prepare environment for CDK usage |
Optional: Add More Resources
You can add more AWS services by importing their modules and defining them inside the constructor
, like:
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as apigateway from 'aws-cdk-lib/aws-apigateway';
Summary
- CDK lets you write infra using code (like TypeScript)
- No need to manage raw YAML or JSON
- CDK apps are compiled and synthesized into CloudFormation templates