Ramakrishna
AWS CDK — Infrastructure as Code
3 min readDec 17, 2019

--

In 2019, AWS launched CDK (Cloud Development Kit) framework to model and provision cloud resources using a programming language.
As per the official document here, the supported programming languages are TypeScript, JavaScript, Python, Java, and C#/.Net.

Now, you might be thinking about how this is different from CloudFormation.

Well, the answer is, we can provision resources programmatically with CDK while in CloudFormation, we create templates using JSON or YAML file.

Another point to note here is that this framework provisions cloud resources through CloudFormation. This means that the code you write using CDK will be converted into CloudFormation template. We will discuss this in detail below

In our project, we used CloudFormation templates for configuring
AWS Services API Gateway, Lambda, DynamoDB, S3 etc.
While writing templates, we realized that the more cloud resources you need, the more cumbersome it becomes as the number of lines increases.
It had become very difficult for us to track/modify any changes.
Now, this problem has been solved by CDK by giving the flexibility to configure cloud resources in a familiar programming language.

In this blog, we will discuss more about configuring API Gateway, Lambda, DynamoDB resources using AWS CDK.

Let’s get started to see how we can use this wonderful framework in developing cloud applications.

Create a sample CDK project in Python

Follow the AWS documentation here to get started with CDK.
After installation, run the below command to create a
sample CDK project in python

cdk init sample-app — language python

Configure AWS Lambda Function and API Gateway

To configure any of the AWS Services like API Gateway or Lambda Function etc., we need to use CDK Construct libraries.

What is a Construct Library?

Construct Library allows us to bundle up a group of services/resources into reusable components. AWS CDK has construct libraries which are divided into modules, one for each AWS service.
For Example, to define an API Gateway , we will need to use an API Gateway Construct Library

To understand more about construct libraries and CDK project in any supported language, please follow the CDK Workshop documentation

Can we create custom Construct Libraries?
yes, we can. For example, when we need to count the number of hits to an API and store the result in dynamodb, we can create this as a construct so that this can be attached to any lambda function as a reusable component.
This example is clearly explained here with code samples in the
AWS documentation.

Install AWS Lambda and API Gateway Construct Libraries

pip install aws-cdk.aws-lambda
pip install aws-cdk.aws_apigateway

AWS Lambda and API Gateway configuration code in python with CDK

The below code snippet does the following:

  1. Configures Lambda Function with the handler code from s3 bucket.
  2. Creates RestApi and integrates with the Lambda Function

Note: This code will not work unless you install Construct Library for each AWS Service you configure

Configure Dynamodb table

Install Construct Library for DynamoDB with the below command

pip install aws_cdk.aws_dynamodb

Configure the DynamoDB table with indexes using the below code snippet

Edit the app.py class from sample project as shown below

Before we run the code, make sure of the following

  1. MyCDKAPIGateway class must exists in file hello_stack.py under the package hello in sample project created above
  2. Keep the DynamoDB table configuration in MyCDKAPIGateway class

Run the command below to generate cloudformation template.

cdk synth hello-cdk-1

Note: Here, we used synth command to convert cdk app into CloudFormation template

Finally, you can run this template or execute cdk deploy command to deploy cdk app into AWS environment.
Once execution or deployment is successful, a CloudFormation stack with the list of services(configured using CDK) will be created in the selected region of your AWS environment.

--

--

Ramakrishna
AWS CDK — Infrastructure as Code

Python / Java developer, AWS Solution Architect, Certified in Deep Learning and Experienced in Micro services Architecture