The AWS Cloud Development Kit (AWS CDK) is an open-source software development framework that enables you to define cloud infrastructure in code and provision it using AWS CloudFormation. It provides a high-level object-oriented abstraction that enables the imperative definition of AWS resources with the help of contemporary programming languages.
With AWS CDK, you can:
- Define your infrastructure in code using a programming language you already know, such as TypeScript, Python, Java, or C#.
- Use AWS CDK constructs to represent common AWS resources and services, such as Amazon S3 buckets, Amazon EC2 instances, and AWS Lambda functions.
- Deploy your infrastructure to AWS using AWS CloudFormation.
- Automate your infrastructure deployments using CI/CD pipelines.
Here are some of the benefits of using AWS CDK:
- Consistency and repeatability: AWS CDK ensures that your infrastructure is defined consistently and can be deployed repeatedly. This makes it easier to manage your infrastructure and to track changes over time.
- Reusability: AWS CDK constructs can be reused across different projects. This makes it easier to build complex infrastructure and share your infrastructure definitions with others.
- Efficiency: AWS CDK can automate the deployment of your infrastructure. This can save you time and effort, and it can help to reduce the risk of errors.
The prerequisites for installing AWS CDK:
- Node.js 14.15.0 or later: AWS CDK uses Node.js to run its CLI and to build your infrastructure code.
- A supported programming language: AWS CDK supports TypeScript, Python, Java, and C#. You can choose the programming language that you are most familiar with.
- An AWS account: You need an AWS account to deploy your infrastructure to AWS. You can create an AWS account for free here: https://aws.amazon.com/free/.
- AWS CLI: The AWS CLI is a command-line tool that you can use to interact with AWS services. You can install the AWS CLI from the AWS CLI website: https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html.
Node.js versions 13.0.0 through 13.6.0 are not compatible with the AWS CDK due to compatibility issues with its dependencies.
Here are some of the steps involved in using AWS CDK:
Install the AWS CDK.
npm install cdk
Create a new project.
cdk init my-cdk-project --language typescript
Once you have created a new project, you can start defining your infrastructure in the lib/index.js
file.
import * as cdk from '@aws-cdk/core';
import * as s3 from '@aws-cdk/aws-s3';
export class HelloCdkStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const bucket = new s3.Bucket(this, 'MyFirstBucket');
}
}
Synthesize an AWS CloudFormation template for the app, as follows.
cdk synth
The cdk synth
the command executes your app, which causes the resources defined in it to be translated into an AWS CloudFormation template. The displayed output of cdk synth
is a YAML-format template. Following, you can see the beginning of our app’s output. The template is also saved in the cdk.out
directory in JSON format.
Resources:
MyFirstBucketB8884501:
Type: AWS::S3::Bucket
Properties:
VersioningConfiguration:
Status: Enabled
UpdateReplacePolicy: Retain
DeletionPolicy: Retain
Metadata:...
As with cdk synth
, you don’t need to specify the name of the stack since there’s only one in the app.
Deploy your infrastructure to AWS.
cdk deploy
cdk deploy
displays progress information as your stack is deployed. When it’s done, the command prompt reappears. You can go to the AWS CloudFormation console and see that it now lists HelloCdkStack
. You’ll also find MyFirstBucket
in the Amazon S3 console.