Back to Blog
Lesson 5 of the AWS: AWS Core Services for Developers course
Cloud NativeJuly 4, 20264 min read

Automating Infrastructure with AWS CDK: A TypeScript Guide

Master AWS CDK to define infrastructure with TypeScript. Learn to initialize projects, manage resources, and deploy stacks faster than raw YAML templates.

AWS CDKTypeScriptInfrastructure as CodeCloud ComputingDevOps

Previously in this course, we explored the fundamentals of Infrastructure as Code with AWS CloudFormation. While writing YAML templates gives you a solid understanding of how AWS resources are structured, it can quickly become verbose and difficult to maintain as your architecture grows. Today, we’re leveling up by using the AWS CDK (Cloud Development Kit) to define our infrastructure using TypeScript.

Why AWS CDK?

Infrastructure as Code (IaC) is about treating your cloud environment like software. When you use raw CloudFormation, you are essentially writing a static configuration file. With the AWS CDK, you use familiar programming constructs—like classes, loops, and conditional logic—to define your stack. The CDK then "synthesizes" your code into CloudFormation templates under the hood, giving you the best of both worlds: the power of a real programming language and the reliability of the CloudFormation deployment engine.

Prerequisites

Before we start, ensure you have:

  1. The AWS CLI configured (see Configuring the AWS CLI).
  2. Node.js (v18+) installed on your machine.
  3. The CDK CLI installed globally: npm install -g aws-cdk.

Initializing Your CDK Project

Let’s set up our project directory. Open your terminal and run the following commands:

Bash
mkdir my-cdk-app
cd my-cdk-app
cdk init app --language typescript

This generates a standard project structure. The most important file for us is lib/my-cdk-app-stack.ts. This is where we will define our infrastructure.

Defining a Simple Resource

Let’s define an S3 bucket—a foundational building block for our serverless project. Open lib/my-cdk-app-stack.ts and update the constructor:

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

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

    // Defining our S3 bucket
    new s3.Bucket(this, CE9178">'MyFirstBucket', {
      versioned: true,
      removalPolicy: cdk.RemovalPolicy.DESTROY, // Use with caution in prod
      autoDeleteObjects: true,
    });
  }
}

By passing this as the first argument, we attach the bucket to our stack. The CDK handles all the boilerplate—like generating unique IDs and setting up default security configurations—that you would have had to write manually in YAML.

Deploying the Stack

Before deploying, we need to "bootstrap" our environment (this creates a hidden S3 bucket that the CDK uses to store deployment artifacts):

Bash
cdk bootstrap

Once that completes, deploy your infrastructure:

Bash
cdk deploy

The CLI will show you a summary of the changes (Security changes, IAM role creation, etc.) and ask for confirmation. Type y to proceed. Once finished, you’ll see your S3 bucket created in the AWS Console.

Hands-on Exercise

  1. Add a second resource: Modify your code to add an SQS Queue to your stack. (Hint: look up aws-sqs in the AWS CDK API Reference).
  2. Synthesize: Run cdk synth to see the actual CloudFormation template generated by your TypeScript code.
  3. Cleanup: Run cdk destroy to remove the resources and avoid unnecessary costs.

Common Pitfalls

  • Forgetting to Bootstrap: If you see "stack not found" or permission errors on your first deployment, ensure you ran cdk bootstrap.
  • Hardcoded Names: Avoid hardcoding bucket names. Let the CDK generate them to prevent collisions, or use removalPolicy: cdk.RemovalPolicy.DESTROY while learning so you don't end up with hundreds of "orphan" buckets.
  • Drift: If you manually change settings in the AWS Console after deploying with CDK, the CDK might try to revert those changes during the next deployment. Always treat your code as the "Source of Truth," similar to how we manage Terraform Infrastructure as Code drift detection.

FAQ

Q: Do I need to know CloudFormation to use CDK? A: You don't need to write it, but understanding the underlying concepts (Stacks, Resources, Outputs) is essential for troubleshooting.

Q: Can I use other languages? A: Yes! CDK supports Python, Java, C#, and Go. TypeScript is the most widely used in the community.

Q: Is CDK production-ready? A: Absolutely. It is the standard for modern AWS deployments, especially for serverless architectures.

Recap

We’ve moved from static YAML to dynamic TypeScript using the AWS CDK. You’ve initialized a project, defined an S3 bucket, and deployed it to your AWS account. This approach is highly repeatable and provides type safety, making your infrastructure much easier to manage at scale.

Up next: We'll begin our journey into serverless compute by exploring the Introduction to AWS Lambda.

Similar Posts