Getting Started with AWS CDK
An introduction to AWS CDK for infrastructure as code.

Getting Started with AWS CDK
I've been involved with Amazon Web Services for over seven years, and throughout this time, the cloud landscape has evolved significantly. However, one constant has been the importance of Infrastructure as Code (IaC) as a fundamental aspect of a successful AWS implementation.
Nowadays, I often find it quicker to kick off all my applications or even proof-of-concept projects using an IaC tool right from the start. Time and again, I've discovered that revisiting projects weeks or months later is much easier when I have a familiar baseline and context to work from.
Personally, I prefer using SST, which builds the entire website. It has been incredibly helpful for deploying Lambdas quickly, validating them promptly, and seamlessly deploying the Next.js frontend application with CloudFront.
I want to focus on AWS CDK because, in my workplace, we needed an alternative to Serverless. The Serverless Framework began charging organizations with annual revenues exceeding $2 million, and since many of our microservice middleware REST APIs are developed in AWS Lambda using Serverless, we needed a way to transition our IaC from Serverless to another option quickly.
IaC Options Considered
-
Serverless: We've been using serverless solutions for quite a while now. However, the recent updates to the Serverless V4 license have sparked some worries.
-
Terraform: A tool for setting up infrastructure that helps users manage resources across different cloud platforms, but it does come with some downsides including a steep learning curve and tricky state management.
-
SAM, Pulumi and SST: SAM looks like a great option, but it lacks comprehensive documentation. Pulumi and SST require a license for enterprise use.
-
CloudFormation: Building a Lambda layer with CloudFormation can be somewhat tricky, especially for newcomers.
Introduction to AWS CDK
- AWS CDK, short for AWS Cloud Development Kit, is an open-source tool that helps developers build cloud infrastructure using modern programming languages.
- Instead of sticking to the usual YAML or JSON formats found in AWS CloudFormation, developers can work with more user-friendly languages like JavaScript/TypeScript, Python, Java, and C#.
- Under the hood, AWS CDK uses the AWS SDK to communicate with AWS services.
- AWS CDK builds on AWS CloudFormation to provision resources in a secure and consistent manner.
How AWS CDK Works
The Cloud Development Kit (CDK) provides a user-friendly way to define your infrastructure using an object-oriented approach. The AWS Construct Library includes a range of constructs that represent different AWS services.
CDK Constructs
AWS CDK constructs are basically building blocks for the cloud that bundle together the configuration and logic for different AWS services.
-
L1 Constructs: These are the basic building blocks which directly represent all the resources available in AWS CloudFormation.
-
L2 Constructs: The next level wraps AWS resources in a more sophisticated intent-based API with some defaults and boilerplate code.
-
L3 Constructs: The top tier consists of patterns designed to help users perform common tasks in AWS.
CDK Workflows
A standard AWS CDK workflow typically starts with setting up a project:
cdk bootstrap: Sets up the CDK toolkit stack in your AWS environmentcdk init: Creates a new CDK projectcdk synth: Synthesizes and displays the CloudFormation templatecdk diff: Shows the changes made since the last deploymentcdk deploy: Deploys your stack(s) to your AWS accountscdk destroy: Removes the stack(s)
CDK Stacks and Apps
CDK Stacks
A stack is basically the core building block you use in AWS CDK. Everything you include in a stack gets set up together as one unit.
import * as cdk from "@aws-cdk/core";
export class CdkPlayStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// resources
}
}
CDK Apps
All AWS resource constructs need to be created inside a stack construct.
import { App } from "@aws-cdk/core";
import { CdkPlayStack } from "./cdk-play-stack";
const app = new App();
new CdkPlayStack(app, "hello-cdk");
app.synth();
AWS CDK Advantages
CDK Benefits
- Language Proficiency: Use popular programming languages like Python, TypeScript, and Java
- Programming Capability: Take advantage of loops, conditionals, and other abstractions
- Unified Deployment: Smooth connection with AWS services makes serverless deployments a breeze
- Deep AWS Integration: Works seamlessly with a variety of AWS services
- Security Best Practices: Automatically handle IAM permissions based on resource relationships
Final Considerations
The AWS Cloud Development Kit (CDK) is a solution that addresses the challenges associated with CloudFormation while retaining its beneficial features. Its primary limitation lies in its exclusive compatibility with AWS.