What is AWS CDK?
- Duong Hoang
- Jun 2, 2024
- 5 min read
AWS Cloud Development Kit
Infrastructure as Code (IaC) has become a standard practice in recent years, providing organizations with the convenience and benefits of managing resources through IaC rather than the traditional AWS console.
Currently, there are two popular tools that allow you to define and manage cloud resources using a declarative approach: AWS CloudFormation and Terraform. However, as your system grows, maintaining template files using JSON or YAML can become cumbersome and challenging.
This is where AWS CDK steps in to improve the workflow for developers, enabling them to create resources and infrastructure on the cloud using an imperative approach.
In this guide, you will learn what AWS CDK is, how you can create resources using code, and best practices during development.

What is AWS Cloud Development Kit (AWS CDK)?
AWS Cloud Development Kit (AWS CDK) is an open-source software framework for defining cloud application resources using familiar programming languages. It synthesizes AWS CDK applications into CloudFormation templates, deploying infrastructure on the AWS Cloud.
The first public beta version of AWS CDK (v.0.8.0) was announced in 2018, introducing an alternative approach to deploying AWS resources using TypeScript. AWS CDK is an open-source project, and its Generally Available (GA) version, AWS CDK v2, was announced by Werner Vogels, CTO of Amazon, in December 2021 at the re
event. CDK v2 supports the following languages:
Language | Stability |
TypeScript | Stable |
JavaScript | Stable |
Python | Stable |
Java | Stable |
C#/.NET | Stable |
Go | Experimental |
With the GA release, no major changes are expected, making it reliable for production environments.
Overview of AWS CDK
Simply put, an AWS CDK application consists of three main components (basic building blocks):
App: The main component that consolidates all stacks within the application and then deploys them on AWS.
Stack: Similar to a stack in CloudFormation, it is a unit (template) containing AWS resources in the form of constructs that can be deployed.
Construct: The basic unit that contains one or more AWS resources. You can create and combine resources to form your own constructs.
AWS CDK Diagram
AWS CDK helps you write code, which the AWS CDK CLI then synthesizes into a CloudFormation template, essentially acting as "assembly code" containing all system components. This template can then be deployed to AWS via CloudFormation.
Apart from the AWS CDK Core framework and AWS CDK CLI, AWS also offers the AWS Construct Hub Library—a repository of AWS CDK constructs for AWS services. The Construct Hub is a resource to discover additional constructs from AWS, third parties, and the open-source community.
Overview of AWS CDK Constructs
As illustrated in the diagram, multiple stacks can be created from multiple constructs, each containing various AWS resources. Constructs are the most convenient feature AWS CDK offers to developers.
Types of Constructs in AWS CDK
L1 Construct: Essentially the same as resources in CloudFormation, with properties mapped directly. If only L1 constructs are used, there are no advantages over CloudFormation. However, if L2 or L3 constructs don't allow you to customize a specific property, you can fall back to using L1 constructs.
L2 Construct: AWS resources developed by the AWS CDK team, packaging L1 constructs with best practices for security and optimized default values. Think of them as boilerplates for resources, enabling you to create AWS resources safely with just a few lines of code.
L3 Construct / Patterns: Also known as patterns, these are groups of L2 constructs combined to provide a complete solution. For example, grouping multiple resources to form a three-tier web application with EC2 instances for backend and frontend linked to RDS, and an Application Load Balancer for load balancing. These can be initialized with a few lines of code and reused across multiple projects or stacks.
Features of AWS CDK
Supports multiple popular programming languages
DRY principle: Avoid repetitive code for commonly used resource groups in your project thanks to L2 and L3 CDK constructs.
Capability to create large CloudFormation templates with minimal code: Templates can be reused multiple times.
Store infrastructure configuration in one repo, using a single AWS CDK application
Direct feedback and contribution to the development team, as it is an open-source project
Utilize familiar languages: Write logic to deploy resources using the same language and create unit tests (e.g., use loops to create multiple similar Lambda functions without duplicating code).
AWS CDK vs. CloudFormation
In the early days of IaC, particularly with CloudFormation, there was limited knowledge sharing or discussion due to its complexity. Most documentation was found on AWS guides. However, AWS CDK has a large community and a Construct library. Additional differences include:
Using L2/L3 Constructs: They utilize best practices, speeding up and securing development compared to configuring individual settings for each resource in CloudFormation.
Using programming languages instead of YAML or JSON: Time spent on programming languages is more beneficial for your career than writing YAML code for CloudFormation.
Starting with AWS CDK
To start, install the AWS CDK toolkit using Node Package Manager (npm). Detailed instructions can be found in the guide below. Briefly, you need to run the following command:
bash
Copy code
npm install -g aws-cdk
AWS CDK Best Practices
Create multiple environments using separate AWS accounts (e.g., Dev, Test, Stage, Prod) for deploying AWS CDK applications.
Add integration tests (automated) in the pipeline to minimize the risk of breaking changes.
Use cdk diff to review changes before deploying to production.
Separate stateful constructs (e.g., databases, storage) from stateless constructs (e.g., APIs, ECS, Monitoring). Enable termination protection for stateful resources, allowing safe deletion and recreation of stateless stacks without data loss.
Avoid placing stateful resources in constructs likely to be moved or renamed (unless data can be recreated, like a cache).
Use control structures and conditions in your chosen programming language to initialize constructs during synthesis. Avoid using CloudFormation logic such as Conditions, {Fn }, or Parameters.
Refer to the official documentation for more best practices.
AWS CDK Development Tools
While AWS CDK saves time, the following tools can further streamline your workflow:
AWS Toolkit for VS Code: Provides easy access to AWS CDK resources in a tree view.
Projen: A project generator for AWS CDK, using best practices to quickly develop new projects.
IntelliCode: Offers AI-powered code completion for constructing Constructs.
Cdk-nag: Validates your application against predefined rules or best practices, integratable into your pipeline to ensure high standards for AWS resources.
FAQ
Is AWS CDK better than Terraform? Terraform and CloudFormation use a declarative approach, while AWS CDK uses an imperative approach. Both are excellent tools with large ecosystems and communities. Terraform uses its own syntax (HCL), whereas AWS CDK lets you choose a familiar programming language. Note that Terraform has also introduced its own CDK.
Is AWS CDK Infrastructure as Code (IaC)? Yes, AWS CDK defines infrastructure through code, specifically generating CloudFormation templates.
What is the potential of AWS CDK? Many developers have adopted AWS CDK, resulting in a larger community than CloudFormation. L2/L3 Constructs enable developers to create systems using best practices without deep AWS service knowledge. Being an open-source project, its development and adoption are rapid. Sharing patterns/templates is easier compared to CloudFormation.
Comments