What is the AWS CDK? - AWS Cloud Development Kit (AWS CDK) v2

This is the AWS CDK v2 Developer Guide. The older CDK v1 entered maintenance on June 1, 2022 and ended support on June 1, 2023.

What is the AWS CDK?

The AWS Cloud Development Kit (AWS CDK) is an open-source software development framework for defining cloud infrastructure in code and provisioning it through AWS CloudFormation.

The AWS CDK consists of two primary parts:

  • AWS CDK Construct Library – A collection of pre-written modular and reusable pieces of code, called constructs, that you can use, modify, and integrate to develop your infrastructure quickly. The goal of the AWS CDK Construct Library is to reduce the complexity required to define and integrate AWS services together when building applications on AWS.

  • AWS CDK Command Line Interface (AWS CDK CLI) – A command line tool for interacting with CDK apps. Use the CDK CLI to create, manage, and deploy your AWS CDK projects. The CDK CLI is also referred to as the CDK Toolkit.

The AWS CDK supports TypeScript, JavaScript, Python, Java, C#/.Net, and Go. You can use any of these supported programming languages to define reusable cloud components known as constructs. You compose these together into stacks and apps. Then, you deploy your CDK applications to AWS CloudFormation to provision or update your resources.

AWS CDK app structure with stacks, constructs, and deployment to CloudFormation.

Benefits of the AWS CDK

Use the AWS CDK to develop reliable, scalable, cost-effective applications in the cloud with the considerable expressive power of a programming language. This approach yields many benefits, including:

Develop and manage your infrastructure as code (IaC)

Practice infrastructure as code to create, deploy, and maintain infrastructure in a programmatic, descriptive, and declarative way. With IaC, you treat infrastructure the same way developers treat code. This results in a scalable and structured approach to managing infrastructure. To learn more about IaC, see Infrastructure as code in the Introduction to DevOps on AWS Whitepaper.

With the AWS CDK, you can put your infrastructure, application code, and configuration all in one place, ensuring that you have a complete, cloud-deployable system at every milestone. Employ software engineering best practices such as code reviews, unit tests, and source control to make your infrastructure more robust.

Define your cloud infrastructure using general-purpose programming languages

With the AWS CDK, you can use any of the following programming languages to define your cloud infrastructure: TypeScript, JavaScript, Python, Java, C#/.Net, and Go. Choose your preferred language and use programming elements like parameters, conditionals, loops, composition, and inheritance to define the desired outcome of your infrastructure.

Use the same programming language to define your infrastructure and your application logic.

Receive the benefits of developing infrastructure in your preferred IDE (Integrated Development Environment), such as syntax highlighting and intelligent code completion.

Code snippet showing AWS CDK setup for ECS cluster with VPC and Fargate service configuration.
Deploy infrastructure through AWS CloudFormation

AWS CDK integrates with AWS CloudFormation to deploy and provision your infrastructure on AWS. AWS CloudFormation is a managed AWS service that offers extensive support of resource and property configurations for provisioning services on AWS. With AWS CloudFormation, you can perform infrastructure deployments predictably and repeatedly, with rollback on error. If you are already familiar with AWS CloudFormation, you don’t have to learn a new IaC management service when getting started with the AWS CDK.

Get started developing your application quickly with constructs

Develop faster by using and sharing reusable components called constructs. Use low-level constructs to define individual AWS CloudFormation resources and their properties. Use high-level constructs to quickly define larger components of your application, with sensible, secure defaults for your AWS resources, defining more infrastructure with less code.

Create your own constructs that are customized for your unique use cases and share them across your organization or even with the public.

Example of the AWS CDK

The following is an example of using the AWS CDK Constructs Library to create an Amazon Elastic Container Service (Amazon ECS) service with AWS Fargate (Fargate) launch type. For more details of this example, see Example: Create an AWS Fargate service using the AWS CDK.

TypeScript
export class MyEcsConstructStack extends Stack { constructor(scope: App, id: string, props?: StackProps) { super(scope, id, props); const vpc = new ec2.Vpc(this, "MyVpc", { maxAzs: 3 // Default is all AZs in region }); const cluster = new ecs.Cluster(this, "MyCluster", { vpc: vpc }); // Create a load-balanced Fargate service and make it public new ecs_patterns.ApplicationLoadBalancedFargateService(this, "MyFargateService", { cluster: cluster, // Required cpu: 512, // Default is 256 desiredCount: 6, // Default is 1 taskImageOptions: { image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample") }, memoryLimitMiB: 2048, // Default is 512 publicLoadBalancer: true // Default is false }); } }
JavaScript
class MyEcsConstructStack extends Stack { constructor(scope, id, props) { super(scope, id, props); const vpc = new ec2.Vpc(this, "MyVpc", { maxAzs: 3 // Default is all AZs in region }); const cluster = new ecs.Cluster(this, "MyCluster", { vpc: vpc }); // Create a load-balanced Fargate service and make it public new ecs_patterns.ApplicationLoadBalancedFargateService(this, "MyFargateService", { cluster: cluster, // Required cpu: 512, // Default is 256 desiredCount: 6, // Default is 1 taskImageOptions: { image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample") }, memoryLimitMiB: 2048, // Default is 512 publicLoadBalancer: true // Default is false }); } } module.exports = { MyEcsConstructStack }
Python
class MyEcsConstructStack(Stack): def __init__(self, scope: Construct, id: str, **kwargs) -> None: super().__init__(scope, id, **kwargs) vpc = ec2.Vpc(self, "MyVpc", max_azs=3) # default is all AZs in region cluster = ecs.Cluster(self, "MyCluster", vpc=vpc) ecs_patterns.ApplicationLoadBalancedFargateService(self, "MyFargateService", cluster=cluster, # Required cpu=512, # Default is 256 desired_count=6, # Default is 1 task_image_options=ecs_patterns.ApplicationLoadBalancedTaskImageOptions( image=ecs.ContainerImage.from_registry("amazon/amazon-ecs-sample")), memory_limit_mib=2048, # Default is 512 public_load_balancer=True) # Default is False
Java
public class MyEcsConstructStack extends Stack { public MyEcsConstructStack(final Construct scope, final String id) { this(scope, id, null); } public MyEcsConstructStack(final Construct scope, final String id, StackProps props) { super(scope, id, props); Vpc vpc = Vpc.Builder.create(this, "MyVpc").maxAzs(3).build(); Cluster cluster = Cluster.Builder.create(this, "MyCluster") .vpc(vpc).build(); ApplicationLoadBalancedFargateService.Builder.create(this, "MyFargateService") .cluster(cluster) .cpu(512) .desiredCount(6) .taskImageOptions( ApplicationLoadBalancedTaskImageOptions.builder() .image(ContainerImage .fromRegistry("amazon/amazon-ecs-sample")) .build()).memoryLimitMiB(2048) .publicLoadBalancer(true).build(); } }
C#
public class MyEcsConstructStack : Stack { public MyEcsConstructStack(Construct scope, string id, IStackProps props=null) : base(scope, id, props) { var vpc = new Vpc(this, "MyVpc", new VpcProps { MaxAzs = 3 }); var cluster = new Cluster(this, "MyCluster", new ClusterProps { Vpc = vpc }); new ApplicationLoadBalancedFargateService(this, "MyFargateService", new ApplicationLoadBalancedFargateServiceProps { Cluster = cluster, Cpu = 512, DesiredCount = 6, TaskImageOptions = new ApplicationLoadBalancedTaskImageOptions { Image = ContainerImage.FromRegistry("amazon/amazon-ecs-sample") }, MemoryLimitMiB = 2048, PublicLoadBalancer = true, }); } }
Go
func NewMyEcsConstructStack(scope constructs.Construct, id string, props *MyEcsConstructStackProps) awscdk.Stack { var sprops awscdk.StackProps if props != nil { sprops = props.StackProps } stack := awscdk.NewStack(scope, &id, &sprops) vpc := awsec2.NewVpc(stack, jsii.String("MyVpc"), &awsec2.VpcProps{ MaxAzs: jsii.Number(3), // Default is all AZs in region }) cluster := awsecs.NewCluster(stack, jsii.String("MyCluster"), &awsecs.ClusterProps{ Vpc: vpc, }) awsecspatterns.NewApplicationLoadBalancedFargateService(stack, jsii.String("MyFargateService"), &awsecspatterns.ApplicationLoadBalancedFargateServiceProps{ Cluster: cluster, // required Cpu: jsii.Number(512), // default is 256 DesiredCount: jsii.Number(5), // default is 1 MemoryLimitMiB: jsii.Number(2048), // Default is 512 TaskImageOptions: &awsecspatterns.ApplicationLoadBalancedTaskImageOptions{ Image: awsecs.ContainerImage_FromRegistry(jsii.String("amazon/amazon-ecs-sample"), nil), }, PublicLoadBalancer: jsii.Bool(true), // Default is false }) return stack }

This class produces an AWS CloudFormation template of more than 500 lines. Deploying the AWS CDK app produces more than 50 resources of the following types.

AWS CDK features

The AWS CDK GitHub repository

For the official AWS CDK GitHub repository, see aws-cdk. Here, you can submit issues, view our license, track releases, and more.

Because the AWS CDK is open-source, the team encourages you to contribute to make it an even better tool. For details, see Contributing to the AWS Cloud Development Kit (AWS CDK).

The AWS CDK API reference

The AWS CDK Construct Library provides APIs to define your CDK application and add CDK constructs to the application. For more information, see the AWS CDK API Reference.

The Construct Programming Model

The Construct Programming Model (CPM) extends the concepts behind the AWS CDK into additional domains. Other tools using the CPM include:

The Construct Hub

The Construct Hub is an online registry where you can find, publish, and share open-source AWS CDK libraries.

Next steps

To get started with using the AWS CDK, see Getting started with the AWS CDK.

Learn more

To continue learning about the AWS CDK, see the following:

To learn more about related topics to the AWS CDK, see the following:

  • AWS CloudFormation concepts – Since the AWS CDK is built to work with AWS CloudFormation, we recommend that you learn and understand key AWS CloudFormation concepts.

  • AWS Glossary – Definitions of key terms used across AWS.

To learn more about tools related to the AWS CDK that can be used to simplify serverless application development and deployment, see the following:

  • AWS Serverless Application Model – An open-source developer tool that simplifies and improves the experience of building and running serverless applications on AWS.

  • AWS Chalice – A framework for writing serverless apps in Python.