Skip to main content

Command Palette

Search for a command to run...

AWS CDK

Updated
4 min read
AWS CDK
N
🚀 Greetings World! 🌐 Meet a dynamic Frontend Developer, UI/UX Designer, and avid explorer of Cloud & DevOps realms! Uncover the journey of a professional deeply passionate about crafting seamless user experiences, designing visually stunning interfaces, and navigating the cloud with a DevOps mindset. 🔧 Skills Snapshot: - Frontend Mastery: HTML, CSS, and JavaScript expert, specializing in React, Angular, and Vue.js. - Design Wizardry: Proficient in wireframing, prototyping, and Adobe Creative Suite and Figma for captivating designs. - Cloud Maestro: Fluent in AWS, Azure, and Google Cloud Platform, adept at architecting scalable solutions. - DevOps Guru: Skilled in Docker, Kubernetes, Jenkins, and Git, contributing to efficient development workflows. 🔗 Let's Connect: Open to collaborating on exciting projects and sharing industry insights, I invite connections for networking or discussions. Reach out for potential collaborations. 📧 Contact Me: -Portfolio:[https://www.nehalingole.in/] - GitHub: [GitHub Profile](https://github.com/Ingole712521) - Email: [nehalingole2001@gmail.com](mailto:nehalingole2001@gmail.com) - Mobile: 7397966719 - Figma: [Figma Profile](https://www.figma.com/@nehalingole) - Twitter: [Twitter Profile](https://twitter.com/IngoleNehal) - HashNode: [HashNode Profile](https://hashnode.com/@Nehal71) - LinkedIn : [LinkedIn Profile](https://www.linkedin.com/in/nehal-ingole/)

When people start using AWS, they usually begin with the console.

Click here → create bucket Click there → create server

It works, but after a while:

  • You forget what you created

  • You can’t track changes

  • You can’t reuse anything

That’s where AWS CDK (Cloud Development Kit) comes in.


What is AWS CDK? (Theory First)

AWS CDK is based on a concept called:

Infrastructure as Code (IaC)

What is Infrastructure as Code?

Instead of manually creating cloud resources, you:

  • Write code to define infrastructure

  • Store it in Git

  • Deploy it anytime

Think of it like this:

If your app is code, your infrastructure should also be code.


Declarative vs Imperative (Important Theory)

This is where most beginners get confused.

Declarative (CloudFormation)

You describe what you want:

Resources:
  MyBucket:
    Type: AWS::S3::Bucket

AWS decides how to create it.


Imperative (Traditional Programming)

You write step-by-step instructions:

createBucket();
enableVersioning();
configureAccess();

Where CDK fits

CDK feels imperative (because you write code), but actually generates declarative CloudFormation behind the scenes.

So it gives you the best of both worlds.


How CDK Works Internally

  1. You write TypeScript code

  2. CDK converts it into CloudFormation JSON

  3. AWS deploys it

This process is called synthesis.

cdk synth

Core Concepts (Theory + Practical)

App

The root of everything.

const app = new cdk.App();

Think of it as your main program.


Stack

A stack is a unit of deployment.

Internally, each stack maps to a CloudFormation stack.

Why it matters:

  • Easier management

  • Easier deletion

  • Logical separation


Construct (Most Important)

Constructs are building blocks.

There are three levels:

L1 (Low-level)

Direct CloudFormation mapping Very detailed, rarely used

L2 (Most used)

Higher-level abstraction Example: s3.Bucket

L3 (Patterns)

Pre-built architecture Example: full API setup


Modern TypeScript Code (CDK v2)

Entry Point (bin/app.ts)

#!/usr/bin/env node
import * as cdk from 'aws-cdk-lib';
import { MyStack } from '../lib/my-stack';

const app = new cdk.App();

new MyStack(app, 'MyStack', {
  env: {
    account: process.env.CDK_DEFAULT_ACCOUNT,
    region: process.env.CDK_DEFAULT_REGION,
  },
});

Stack (lib/my-stack.ts)

import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as s3 from 'aws-cdk-lib/aws-s3';

export class MyStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    new s3.Bucket(this, 'MyBucket', {
      versioned: true,
      removalPolicy: cdk.RemovalPolicy.DESTROY,
      autoDeleteObjects: true,
    });
  }
}

Lifecycle of CDK (Theory)

1. Write Code

Define infrastructure in TypeScript

2. Synthesize

cdk synth

Converts to CloudFormation template


3. Deploy

cdk deploy

Creates or updates resources


4. Diff

cdk diff

Shows changes before deploying


5. Destroy

cdk destroy

Deletes all resources


CDK vs CloudFormation (Theory Comparison)

Feature CDK CloudFormation
Language TypeScript, Python, etc. JSON/YAML
Reusability High Low
Readability Easier Verbose
Learning Curve Moderate Steeper

CDK is a developer-friendly layer over CloudFormation.


Important Design Principles

Idempotency

Running cdk deploy multiple times should not break anything. Only changes are applied.


State Management

AWS manages infrastructure state using CloudFormation. You don’t handle it manually.


Abstraction

CDK simplifies:

  • IAM permissions

  • Resource dependencies

  • Configuration complexity


Real Example: Lambda + API

import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as apigateway from 'aws-cdk-lib/aws-apigateway';

const fn = new lambda.Function(this, 'MyFunction', {
  runtime: lambda.Runtime.NODEJS_20_X,
  handler: 'index.handler',
  code: lambda.Code.fromAsset('lambda'),
});

new apigateway.LambdaRestApi(this, 'MyApi', {
  handler: fn,
});

This creates:

  • A Lambda function

  • API Gateway endpoint

  • Required permissions automatically


Common Beginner Mistakes

  • Skipping cdk bootstrap

  • Putting everything in one stack

  • Hardcoding values

  • Forgetting to destroy unused resources (cost implications)


When Should You Use CDK?

Use CDK when:

  • Building real-world applications

  • Working in teams

  • Needing automation and scalability

Avoid it if you are only exploring AWS basics.


Final Thoughts

AWS CDK represents a shift from manual setup to engineered systems.

Instead of clicking through the console, you define everything in code, making it:

  • Repeatable

  • Version-controlled

  • Scalable

If you already know TypeScript, CDK will feel natural and powerful.