AWS CDK Tutorial, for Beginners


0:00
0:00

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

CommandPurpose
cdk initInitialize a CDK project
cdk synthOutput the CloudFormation template
cdk deployDeploy the stack to AWS
cdk destroyRemove all resources in the stack
cdk diffShow changes between current vs. deployed stack
cdk bootstrapPrepare 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

CommandPurpose
cdk initInitialize a CDK project
cdk synthOutput the CloudFormation template
cdk deployDeploy the stack to AWS
cdk destroyRemove all resources in the stack
cdk diffShow changes between current vs. deployed stack
cdk bootstrapPrepare 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

Last updated on July 3, 2025

šŸ” Explore More Topics

Discover related content that might interest you

TwoAnswers Logo

Providing innovative solutions and exceptional experiences. Building the future.

Ā© 2025 TwoAnswers.com. All rights reserved.

Made with by the TwoAnswers.com team