Learn AWS-CDK with Real Code Examples

Updated Nov 27, 2025

Explain

CDK allows defining AWS infrastructure as constructs, which are reusable, composable building blocks.

It supports multiple languages through language-specific bindings (JSII).

CDK synthesizes code into AWS CloudFormation templates for deployment.

Enables best practices like IAM policies, networking, and resource configuration via code.

Facilitates repeatable, version-controlled, and testable cloud infrastructure.

Core Features

Constructs - reusable building blocks

Stacks - deployable CloudFormation units

Apps - collections of stacks

Context-aware configuration

Cross-stack references and dependencies

Basic Concepts Overview

App - top-level CDK application

Stack - unit of deployment

Construct - building block for AWS resources

Environment - AWS account and region

Context - configuration values for stacks

Project Structure

cdk.json - project configuration

bin/ - entry-point app

lib/ - stacks and constructs

package.json / requirements.txt - dependencies

cdk.context.json - cached context data

Building Workflow

Create a new CDK project using CLI

Write constructs for required AWS resources

Add stacks and instantiate constructs

Synthesize the project into CloudFormation templates

Deploy stacks to AWS using CDK CLI

Difficulty Use Cases

Beginner: Deploy a single S3 bucket

Intermediate: Multi-stack VPC + RDS deployment

Advanced: Complex multi-environment pipelines

Expert: Custom L1/L2/L3 constructs for reusable infra

Architect: Enterprise-wide CDK construct libraries

Comparisons

CDK vs CloudFormation: CDK provides code abstraction and reusable constructs

CDK vs Terraform: CDK is AWS-native, Terraform is multi-cloud

CDK vs Serverless Framework: CDK supports full AWS stack, Serverless is function-focused

CDK vs Pulumi: Pulumi supports multi-cloud, CDK is tightly integrated with AWS

CDK vs manual console: CDK is repeatable, version-controlled, and testable

Versioning Timeline

CDK v0.x (2018) - initial developer preview

CDK v1 (2019) - stable multi-language support

CDK v1.x - additions for new AWS services

CDK v2 (2021) - single package for all AWS constructs

2024–2025 - continuous updates for new AWS services

Glossary

App - top-level CDK application

Stack - deployable CloudFormation unit

Construct - building block of infrastructure

Context - environment/configuration values

JSII - multi-language binding framework