Writing infrastructure as code with AWS CDK

Introduction

In this article, we will look at how we can write AWS infrastructure as code using the Cloud Development Kit. This feature may appeal to those who prefer a programmatic approach to infra code.

What is CDK?

CDK can be considered as an alternative to other infra-code tools such as cloud formation(though CDK still generates cloud formation in the background) and terraform(within the context of AWS as terraform can be used for hybrid cloud workloads). It is open-sourced by AWS and, as of today, supports the following programming languages: TypeScript, JavaScript, Java, C# and Python.

At its core CDK lets us define reusable components known as constructs and they can be composed together to build stacks and apps. Constructs are the main building blocks and they can be a single resource(DynamoDB table for example) or a combination of several resources. There are 3 different types of constructs provided by CDK: 1) L1 — these represent low-level resources such as S3 bucket and are named Cfn<resource name>; CfnBucket for example. 2) L2 — these also represent AWS resources but come with high-level API, reasonable defaults and boilerplate code. 3) Patterns — help us build common tasks using multiple resources. Stacks can be thought of as a unit of deployment(like Web layer, DB layer) and are implemented using CloudFormation stacks. All constructs must be defined within the scope of stacks and any number of stacks can be defined in the CDK app. App is a root-level container construct enveloping stacks and constructs.

Example Deployment:

Let’s build a sample serverless app deployment(using TypeScript) to see how these concepts look like in practice. Please be aware that doing the following steps using your AWS account might incur some cost; don’t forget to delete the created resources in the end.

Prerequisites:

1.Install Node.js(version 14 or later)

2. Install typescript(version 2.7 or later) with the following command

3. Create a config file in your home directory if it doesn’t exist already(~/.aws/config) and give a default region like below:

4. Create a credentials file in your home directory if it doesn’t exist already(~/.aws/credentials) and configure access keys like below:

5. Install AWS CDK with the following command:

Deployment Steps:

Our sample serverless app consists of some lambda functions, an API gateway and a DynamoDB table. Basically, we will just create a simple HTTP API to create and get user information(Note: API and business logic doesn’t have any validations in place as our main theme here is to concentrate on infra code).

First, create and initialize our app directory(anywhere you prefer) using the following commands:

Once the above steps are done, load the cdkdemo app to your favourite IDE(use VSCode if possible). The main entry point file(cdkdemo.ts) to our app is located under bin directory. Now let's create resources directory under the project root directory where we will place lambda functions.

Create the following JavaScript files(lambda business logic) under resources directory.

create-user.js

get-user.js

Add API Gateway, Lambda and DynamoDB CDK packages to our app. Run the following command at the project root:

Now let's write the actual infra code to create Gateway, DynamoDB table and Lambda functions. Create a file with the name sample-service.ts under lib folder.

sample-service.ts

Please pay close attention to this code. We are creating all our resources in this service class and thanks to TypeScript we will have compile time checking to ensure we are not missing any mandatory parameters etc. Awesome! Now let's add this service to our stack. Open cdkdemo-stack.ts file under lib and import our sample service into it like below:

As our app contains assets to be uploaded(lambda functions), we need to bootstrap the environment before deploying the app. This will create a staging S3 bucket that the CDK uses to deploy stacks containing assets. Run the following commands in sequence to bootstrap and deploy the apps.

Once the deployment is successful, we can get the API gateway URL from the console logs and test our API with curl or any other REST client.

Sample create and get user requests look like below.

Browse through AWS console to get an understanding of how CDK created resources. Once done, let’s delete the sample app using the following command:

Conclusion

There we have it. With just a few lines of simple code, we were able to deploy a serverless app to AWS. Personally, I prefer a programmatic approach to a declarative approach for writing infra code as we have more power and control to do things. In my opinion, CDK is definitely a great improvement over plain CloudFormation templates and for teams where developers write infra code this will be a great advantage.

engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store