The AWS Cloud Development Kit (CDK) lets you define your cloud infrastructure using familiar programming languages, but its real magic is in how it synthesizes that code into CloudFormation templates.

Let’s build a simple S3 bucket. First, ensure you have Node.js and npm installed, and then install the AWS CDK CLI:

npm install -g aws-cdk

Next, create a new CDK project:

mkdir cdk-s3-bucket
cd cdk-s3-bucket
cdk init app --language typescript

This sets up a basic TypeScript project. Open bin/cdk-s3-bucket.ts. You’ll see a CdkS3BucketStack instance being created. Replace its contents with this:

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

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

    new s3.Bucket(this, 'MyFirstS3Bucket', {
      versioned: true,
      removalPolicy: cdk.RemovalPolicy.DESTROY, // Be careful with this in production!
      autoDeleteObjects: true, // Also be careful!
    });
  }
}

This defines a stack named CdkS3BucketStack. Inside it, we create a new S3 bucket. versioned: true enables versioning, which is great for recovering from accidental deletions or overwrites. removalPolicy: cdk.RemovalPolicy.DESTROY and autoDeleteObjects: true are convenient for development and testing, as they ensure the bucket and its contents are cleaned up when you destroy the stack. Do not use these in production unless you fully understand the implications.

Now, let’s synthesize this into a CloudFormation template. Run:

cdk synth

This command outputs the generated CloudFormation JSON. You’ll see a template that defines an S3 bucket resource with the properties we specified.

To deploy this stack, you first need to bootstrap your AWS account for CDK. This sets up necessary permissions and resources for CDK to manage your stacks.

cdk bootstrap aws://YOUR_ACCOUNT_ID/YOUR_REGION

Replace YOUR_ACCOUNT_ID and YOUR_REGION with your actual AWS account ID and the region you want to deploy to (e.g., 123456789012/us-east-1).

Finally, deploy your stack:

cdk deploy

CDK will show you a summary of the changes it’s about to make (creating an S3 bucket) and ask for confirmation. Type y and press Enter. CDK will then provision the resources in your AWS account. You can verify the S3 bucket creation in the AWS Management Console.

To tear down your resources, run:

cdk destroy

This will remove the S3 bucket and any other resources defined in your stack.

The removalPolicy and autoDeleteObjects properties are not just for convenience; they directly translate into specific attributes within the CloudFormation resource definition that control how CloudFormation handles resource deletion. The cdk synth command is your window into this translation layer, showing you exactly what CloudFormation will see.

The next step is to add more complex resources, like Lambda functions or API Gateways, and see how CDK’s constructs abstract away the underlying CloudFormation complexities.

Want structured learning?

Take the full Cdk course →