Getting started with AWS CDK

September 8, 2021 |  

Photo by Markus Spiske on Unsplash  

What is AWS Cloud Development Kit (AWS CDK)?

AWS CDK is an open-source software development framework to define your cloud application resources using familiar programming languages. Provisioning cloud applications can be a challenging process that requires you to perform manual actions, write custom scripts, maintain templates, or learn domain-specific languages. AWS CDK uses the familiarity and expressive power of programming languages for modeling your applications. It provides you with high-level components called constructs that preconfigure cloud resources with proven defaults, so you can build cloud applications without needing to be an expert. AWS CDK provisions your resources in a safe, repeatable manner through AWS CloudFormation. It also enables you to compose and share your own custom constructs that incorporate your organization’s requirements, helping you start new projects faster.

This blog starts your journey in using AWS CDK and creating Infrastructure as Code (IaC) in AWS. For the first part in the series, you will install AWS CDK along with the required prerequisites. Then we will use the CDK to create and deploy an Amazon S3 bucket in your account.

You can follow the instructions outlined below. The source code to a completed stack along with detailed readme files is available on the Movinture GitHub.

Prerequisites

You must start by installing the prerequisites:

  • Install Node.js – AWS CDK supports creating infrastructure using TypeScript, JavaScript, Python, Java, or C#. Node.js is required for using AWS CDK.
  • You must configure your workstation with your credentials and an AWS region. AWS uses credentials to identify who is calling services and whether access to the requested resources is allowed. In AWS, these credentials are typically the access key ID and the secret access key that were created along with your account. For our tutorials these credentials will be used by AWS CDK command line tools to deploy the infrastructure in the AWS account. You can use the AWS CLI to configure the credentials or set up environment variables.
    • For this blog post tutorial, I am using an AWS CLI profile – `cdkapsouth1` with the region set to ap-south-1 (Mumbai). If you are using default profile – then you can remove the `–profile cdkapsouth1` from the instructions.
  • You should install TypeScript 2.7 or later globally on your workstation.
  • If you are using Python, Java, or C# for coding your infrastructure you have to language specific requirements such installing Python 3.6 or higher, and Java Development Kit (JDK) 8 or higher. We will be using TypeScript to generate infrastructure.

Note: You can follow detailed instructions or use the code in the GitHub repository if you do not want to follow the instructions step by step.

Install AWS CDK

Once you are done installing the prerequisites, you need to install AWS CDK:

npm install -g aws-cdk

And check your CDK version:

cdk –version

The code provided with this tutorial was developed CDK Version 1.120.0.

CDK Bootstrap

Many AWS CDK stacks that you write will include assets: external files that are deployed with the stack, such as AWS Lambda functions or Docker images. The AWS CDK uploads these to an Amazon S3 bucket or other container so they are available to AWS CloudFormation during deployment. Deployment requires that these containers already exist in the account and region you are deploying into. Creating them is called bootstrapping.

Here is an image of CloudFormation console in the region ap-south-1 prior to bootstrapping CDK:

You can bootstrap using the following command:

cdk bootstrap aws://ACCOUNT-NUMBER/REGION

In my case, I used the `cdkapsouth1` profile created to bootstrap:

cdk bootstrap –profile cdkapsouth1

Now if you browse the ap-south-1 CloudFormation console you will see a bucket created for staging resources used by CDK:

Your first CDK application

You will first initialize the CDK project, create the resources and deploy them.

Initialize the CDK application

Now let’s create your first CDK project.

 

  1. Create a folder called as `day01-s3`

mkdir day01-s3

2. Change to this folder, and initialize the CDK app:

cd day01-s3

cdk init app –language=typescript

3. Run the following command to generate the JavaScript files:

npm run build

4. You can type the following commands to list the stacks in this folder, you should get the output as shown below the command:

cdk ls

Day01S3Stack

You will see an output corresponding to the name of the folder you created (if you used anything else other than `day01-s3`). Your first CDK project is initialized, now you can start building out the infrastructure.

 

Creating resources – S3 bucket

  1. To create or manage Amazon S3 buckets in AWS CDK, you first need to install the CDK S3 package as shown below. This will install the latest version of the package.

npm install @aws-cdk/aws-s3

Note: Each individual construct package for AWS resources should be installed at the same version as the AWS CDK version. You can get the version by running the command `cdk –version`.  On my system the version was 1.120.0 and I have an example below to install that specific version.

npm install @aws-cdk/aws-s3@1.120.0

2. Add the following lines at the top of the file `day01-s3-stack.ts`:

import { Bucket, BucketEncryption } from ‘@aws-cdk/aws-s3’;

import { CfnOutput, RemovalPolicy } from ‘@aws-cdk/core’;

3. Add the following lines below the statement “// The code that defines…”:

const s3bucket = new Bucket(this, ‘day01s3bucket’, {

encryption: BucketEncryption.S3_MANAGED,

removalPolicy: RemovalPolicy.DESTROY,

autoDeleteObjects: true

});

 

new CfnOutput(this, ‘S3 Bucket Name’, {

value: s3bucket.bucketName,

description: ‘The name of an S3 bucket’, // Optional

});

You can look up the file on GitHub repo to get the stack file here.

4. Now deploy the stack:

cdk deploy –profile cdkapsouth1

5. The `CfnOutput` command in the stack will lead to a console output on successful deployment as shown below. Note the `XXXXXXXXXXXXX` in the bucket name will be specific to your output since this is chosen (uniquely by the CDK/CloudFormation process for a globally unique bucket name)

Outputs:
Day01S3Stack.S3BucketName = day01s3stack-day01s3bucket099ef4ce-XXXXXXXXXXXX

6. Now if you go to the AWS S3 console you can find the bucket with the name above, here is a screenshot of the bucket created when I deployed the CDK stack on my account. And if you go to the ‘Properties’ tab of the S3 console and scroll down you will see that encryption has been enabled on this bucket.

7. You can now delete the CloudFormation stack, and the resources created by using the command:

cdk destroy –profile cdkapsouth1

Congratulations you have started your journey on using AWS CDK! Keep checking back in Movinture blogs to continue your AWS learning journey!