AWS CDK became Generall Available (GA) in July 2019 with support of TypeScript and Python to begin with. It is an open source development framework. With AWS CDK, you can generate CloudFormation templates and provision infrastructure. As of this writing, AWS CDK supports Python and TypeScript in GA and .NET and Java in Developer Preview mode.
Before AWS CDK, I used Troposphere which is a Python library to create CloudFormation templates. I found that very helpful to generate CloudFormation templates in JSON format in particular.
This is the first article of series of articles based on AWS CDK. Let's first understand what is AWS CDK, how does it work and how you can get started with it.
What is AWS CDK?
AWS CDK is an open source software development framework. Everything in AWS CDK is a construct. To understand better, you can think construct as a cloud component which can also be called as a resource to understand it in the terminology of CloudFormation templates.
For Example, S3 bucket resource is a construct, DynamoDB resource is a construct and so on. You can use constructs to define relationships and set their properties.
Let's dig into it and create your first CloudFormation template using AWS CDK. I'm going to keep it simple and create an S3 bucket template for better understanding.
Create S3 bucket template using AWS CDK
In order to start working with AWS CDK, you need to setup your environment. Well, I'm not going to install anything on my local machine, instead I will configure the environment using Docker. Please checkout my article if you want to know more about configuring Docker with VSCode for Python.
AWS CDK Folder Struture
At the bare minimum, you need to setup your AWS CDK app with three files as listed below:
app.py - A standard file to define your AWS CDK constructs
cdk.json - A standard file to map your app with python executable
requirements.txt - Optional but I recommend keeping this file. It contains all the requirements your app needs to generate template. You can install these requirements using pip.
Here is how each of this file looks like.
Now we have our code ready. In addition to these three files, let's create a new folder called .divcontainer at the root of our app. Let's configure that setup so that we run our code in the container instead of installing everything on local system.
Configure VSCode with Docker
We need create two files in .divcontainer folder, viz. dockercontainer.json and Dockerfile. Let's setup both.
Once you have this setup completed, press F1 in VSCode and select Remote-Containers: Open Folder in Container option. You can also reopen VSCode and it will detect automatically that this projects need to run in the container. Below screenshot explains the entire process.
Once dependencies are installed, it's time to generate the template and deploy in AWS. Below is the list of commands you need to execute to complete deployment.
The command cdk deploy creates a CloudFormation change-set and deploys the template. Below is some more detailed output generated by cdk deploycommand
Finally, here is the list of commonly used AWS CDK commands: