aws-cdk
The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
Top Related Projects
Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
Pulumi - Infrastructure as Code in any programming language 🚀
⚡ Serverless Framework – Effortlessly build apps that auto-scale, incur zero costs when idle, and require minimal maintenance using AWS Lambda and other managed cloud services.
The AWS Provider enables Terraform to manage AWS resources.
Quick Overview
The AWS CDK (Cloud Development Kit) is an open-source software development framework for defining cloud infrastructure in code and provisioning it through AWS CloudFormation. It allows developers to use familiar programming languages to model and provision AWS resources, making it easier to build and manage cloud applications.
Pros
- Supports multiple programming languages (TypeScript, JavaScript, Python, Java, C#, Go)
- Provides high-level constructs that abstract away complex CloudFormation details
- Enables code reuse and sharing through composable and shareable components
- Offers built-in testing capabilities for infrastructure code
Cons
- Steep learning curve for developers new to infrastructure-as-code concepts
- Frequent updates and changes can lead to compatibility issues between versions
- Limited support for non-AWS resources compared to other IaC tools
- Debugging can be challenging due to the abstraction layers
Code Examples
- Creating an S3 bucket:
import * as cdk from 'aws-cdk-lib';
import * as s3 from 'aws-cdk-lib/aws-s3';
const bucket = new s3.Bucket(this, 'MyBucket', {
versioned: true,
encryption: s3.BucketEncryption.S3_MANAGED,
});
- Defining a Lambda function:
import * as lambda from 'aws-cdk-lib/aws-lambda';
const myFunction = new lambda.Function(this, 'MyFunction', {
runtime: lambda.Runtime.NODEJS_14_X,
handler: 'index.handler',
code: lambda.Code.fromAsset('lambda'),
});
- Creating an API Gateway:
import * as apigateway from 'aws-cdk-lib/aws-apigateway';
const api = new apigateway.RestApi(this, 'MyApi', {
restApiName: 'My API Service',
});
const integration = new apigateway.LambdaIntegration(myFunction);
api.root.addMethod('GET', integration);
Getting Started
-
Install the AWS CDK CLI:
npm install -g aws-cdk
-
Create a new CDK project:
mkdir my-cdk-project && cd my-cdk-project cdk init app --language typescript
-
Install dependencies and compile:
npm install npm run build
-
Deploy your stack:
cdk deploy
Competitor Comparisons
Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
Pros of Terraform
- Multi-cloud support, allowing infrastructure management across various providers
- Larger ecosystem with extensive third-party provider plugins
- More mature and established project with a larger community
Cons of Terraform
- Steeper learning curve, especially for those new to infrastructure as code
- Less native integration with AWS services compared to CDK
- HCL syntax can be less intuitive for developers familiar with programming languages
Code Comparison
CDK (TypeScript):
import * as ec2 from 'aws-cdk-lib/aws-ec2';
const vpc = new ec2.Vpc(this, 'MyVPC', {
maxAzs: 2
});
Terraform:
resource "aws_vpc" "my_vpc" {
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true
enable_dns_support = true
}
Both CDK and Terraform are powerful infrastructure as code tools. CDK leverages familiar programming languages and offers deeper AWS integration, while Terraform provides a more cloud-agnostic approach with its own domain-specific language. The choice between them often depends on specific project requirements, team expertise, and the target cloud environment.
Pulumi - Infrastructure as Code in any programming language 🚀
Pros of Pulumi
- Multi-cloud support: Pulumi works with AWS, Azure, GCP, and more
- General-purpose programming languages: Use Python, TypeScript, Go, etc.
- Easier state management with built-in state storage options
Cons of Pulumi
- Steeper learning curve for those familiar with YAML/JSON-based IaC
- Smaller community and ecosystem compared to CDK
- Potential for more complex code due to general-purpose languages
Code Comparison
Pulumi (Python):
import pulumi
import pulumi_aws as aws
bucket = aws.s3.Bucket("my-bucket")
pulumi.export("bucket_name", bucket.id)
CDK (TypeScript):
import * as cdk from '@aws-cdk/core';
import * as s3 from '@aws-cdk/aws-s3';
const bucket = new s3.Bucket(this, 'MyBucket');
new cdk.CfnOutput(this, 'BucketName', { value: bucket.bucketName });
Both Pulumi and CDK offer infrastructure-as-code solutions, but they differ in their approach and target audience. Pulumi provides a more flexible, multi-cloud solution using general-purpose programming languages, while CDK focuses on AWS-specific resources with a syntax closer to CloudFormation. The choice between them depends on your specific needs, existing skillset, and target cloud platforms.
⚡ Serverless Framework – Effortlessly build apps that auto-scale, incur zero costs when idle, and require minimal maintenance using AWS Lambda and other managed cloud services.
Pros of Serverless
- Multi-cloud support (AWS, Azure, GCP, etc.)
- Simpler learning curve for beginners
- Extensive plugin ecosystem
Cons of Serverless
- Less fine-grained control over infrastructure
- Limited to serverless architectures
- Potential vendor lock-in with Serverless Framework
Code Comparison
Serverless:
service: my-service
provider:
name: aws
runtime: nodejs14.x
functions:
hello:
handler: handler.hello
AWS CDK:
import * as cdk from '@aws-cdk/core';
import * as lambda from '@aws-cdk/aws-lambda';
export class MyStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
new lambda.Function(this, 'HelloHandler', {
runtime: lambda.Runtime.NODEJS_14_X,
handler: 'handler.hello',
code: lambda.Code.fromAsset('lambda')
});
}
}
The Serverless Framework uses a YAML configuration file to define services and functions, while AWS CDK uses TypeScript (or other supported languages) to programmatically define infrastructure as code. AWS CDK provides more flexibility and control over resource creation, while Serverless Framework offers a simpler, more opinionated approach focused on serverless architectures.
The AWS Provider enables Terraform to manage AWS resources.
Pros of terraform-provider-aws
- More mature and widely adopted in the industry
- Supports a broader range of AWS services and resources
- Offers a more declarative approach to infrastructure definition
Cons of terraform-provider-aws
- Steeper learning curve for beginners
- Less integrated with AWS-specific features and best practices
- Requires manual state management and can be prone to drift
Code Comparison
terraform-provider-aws:
resource "aws_s3_bucket" "example" {
bucket = "my-bucket-name"
acl = "private"
}
aws-cdk:
import * as s3 from '@aws-cdk/aws-s3';
new s3.Bucket(this, 'MyBucket', {
bucketName: 'my-bucket-name',
versioned: true,
encryption: s3.BucketEncryption.S3_MANAGED,
});
The terraform-provider-aws example uses HCL (HashiCorp Configuration Language) to define an S3 bucket, while the aws-cdk example uses TypeScript with the AWS CDK library. The CDK approach provides more abstraction and includes AWS best practices by default, such as versioning and encryption.
Both tools are powerful for managing AWS infrastructure, but they cater to different preferences and use cases. terraform-provider-aws offers more flexibility and control, while aws-cdk provides a more opinionated and AWS-integrated experience.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
AWS Cloud Development Kit (AWS CDK)
The AWS Cloud Development Kit (AWS CDK) is an open-source software development framework to define cloud infrastructure in code and provision it through AWS CloudFormation.
It offers a high-level object-oriented abstraction to define AWS resources imperatively using the power of modern programming languages. Using the CDKâs library of infrastructure constructs, you can easily encapsulate AWS best practices in your infrastructure definition and share it without worrying about boilerplate logic.
The CDK is available in the following languages:
- JavaScript, TypeScript (Node.js ⥠14.15.0)
- We recommend using a version in Active LTS
- Python (Python ⥠3.8)
- Java (Java ⥠8 and Maven ⥠3.5.4)
- .NET (.NET ⥠6.0)
- Go (Go ⥠1.16.4)
Third-party Language Deprecation: language version is only supported until its EOL (End Of Life) shared by the vendor or community and is subject to change with prior notice.
Jump To:
Developer Guide |
API Reference |
Getting Started |
Getting Help |
Contributing |
RFCs |
Roadmap |
More Resources
Developers use the CDK framework in one of the supported programming languages to define reusable cloud components called constructs, which are composed together into stacks, forming a "CDK app".
They then use the AWS CDK CLI to interact with their CDK app. The CLI allows developers to synthesize artifacts such as AWS CloudFormation Templates, deploy stacks to development AWS accounts and "diff" against a deployed stack to understand the impact of a code change.
The AWS Construct Library includes a module for each AWS service with constructs that offer rich APIs that encapsulate the details of how to use AWS. The AWS Construct Library aims to reduce the complexity and glue-logic required when integrating various AWS services to achieve your goals on AWS.
Modules in the AWS Construct Library are designated Experimental while we build them; experimental modules may have breaking API changes in any release. After a module is designated Stable, it adheres to semantic versioning, and only major releases can have breaking changes. Each module's stability designation is available on its Overview page in the AWS CDK API Reference. For more information, see Versioning in the CDK Developer Guide.
Getting Started
For a detailed walkthrough, see the tutorial in the AWS CDK Developer Guide.
At a glance
Install or update the AWS CDK CLI from npm (requires Node.js ⥠14.15.0). We recommend using a version in Active LTS
npm i -g aws-cdk
(See Manual Installation for installing the CDK from a signed .zip file).
Initialize a project:
mkdir hello-cdk
cd hello-cdk
cdk init sample-app --language=typescript
This creates a sample project looking like this:
export class HelloCdkStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const queue = new sqs.Queue(this, 'HelloCdkQueue', {
visibilityTimeout: cdk.Duration.seconds(300)
});
const topic = new sns.Topic(this, 'HelloCdkTopic');
topic.addSubscription(new subs.SqsSubscription(queue));
}
}
Deploy this to your account:
cdk deploy
Use the cdk
command-line toolkit to interact with your project:
cdk deploy
: deploys your app into an AWS accountcdk synth
: synthesizes an AWS CloudFormation template for your appcdk diff
: compares your app with the deployed stack
Getting Help
The best way to interact with our team is through GitHub. You can open an issue and choose from one of our templates for bug reports, feature requests, documentation issues, or guidance.
If you have a support plan with AWS Support, you can also create a new support case.
You may also find help on these community resources:
- Look through the API Reference or Developer Guide
- The #aws-cdk Slack channel in cdk.dev
- Ask a question on Stack Overflow
and tag it with
aws-cdk
Roadmap
The AWS CDK Roadmap lets developers know about our upcoming features and priorities to help them plan how to best leverage the CDK and identify opportunities to contribute to the project. See ROADMAP.md for more information and FAQs.
Contributing
We welcome community contributions and pull requests. See CONTRIBUTING.md for information on how to set up a development environment and submit code.
Metrics collection
This solution collects anonymous operational metrics to help AWS improve the quality and features of the CDK. For more information, including how to disable this capability, please see the developer guide.
More Resources
- CDK Workshop
- Construct Hub - Find and use open-source Cloud Development Kit (CDK) libraries
- Best Practices
- All developer blog posts about AWS CDK
- CDK Construction Zone - A Twitch live coding series hosted by the CDK team, season one episodes:
- Triggers: Join us as we implement Triggers, a Construct for configuring deploy time actions. Episodes 1-3:
- S1E4: Tokens Deep Dive; Participants: @NetaNir,@rix0rrr, @iliapolo, @RomainMuller
- S1E5: Assets Deep Dive; Participants: @NetaNir, @eladb, @jogold
- S1E6: Best Practices; Participants: @skinny85, @eladb, @rix0rrr, @alexpulver
- S1E7: Tips and Tricks From The CDK Team; Participants: All the CDK team!
- Examples
- Changelog
- NOTICE
- License
Top Related Projects
Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
Pulumi - Infrastructure as Code in any programming language 🚀
⚡ Serverless Framework – Effortlessly build apps that auto-scale, incur zero costs when idle, and require minimal maintenance using AWS Lambda and other managed cloud services.
The AWS Provider enables Terraform to manage AWS resources.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot