life is too short for a diary




Mon 14 Jun 2021

Getting Started with AWS CDK

Tags: aws cdk cloudformation typescript python

Cloudformation service in AWS allows you to describe an entire set of resources required to make a pipeline. The cloudformation template can be described in JSON or YAML format. Updating the cloudformation template was not a pleasant experience. I wanted to discover alternative ways to generate cloudformation template programmatically.

The CDK library provides you a way to declare the resources in your favorite languages like Java, Javascript, Typescript, and Python. In this tutorial, we will use typescript and python to generate our cloudformation template.

Before we begin

Just a quick referesher to few concepts before we delve into AWS CDK

  1. Cloudformation template describes AWS resources. A running instance of cloudformation is called a stack.

  2. Constructs are the basic building blocks of AWS CDK apps.

  3. L1 construct are named CfnXyz, where Xyz is name of the resource. They are low-level construct which directly represent cloudformation resources.

  4. L2 construct provide common boilerplates and glue logic. These will come with convenient defaults and reduces the amount of knowledge you need to know about them.

  5. L3 construct are called high-level patterns. These constructs are designed to help you complete common tasks in AWS, often involving multiple kinds of resources.

For this tutorial, we will use L1 construct. However same concept can be used for L2 construct.


Installation

First, install the AWS CDK CLI from command line using

$ npm i -g aws-cdk@latest

Let’s create a project

Then you can verify


For our first use case, will add Parameters to our cloudformation. We need to edit file lib\test-cdk-stack.ts for typescript or lib\test_cdk_stack.py for python.


We can see the output of the cloudformation template by using the following command

$ cdk synth

If you want to generate cloudformation template without metadata

$ cdk synth --version-reporting false --path-metadata false 

The output is

For CDK v2, CheckBootstrapVersion rule is added to the Stack's template as a safety check, to verify that the bootstrap stack in the target environment meets the minimum requirements of the current stack1.

Stepfunction

The cloudformation template needs at least one resource to validate. We will add a stepfunction to our cloudformation. We will define our stepfunction inside a json file, helloworld.asl.json

{
  "Comment" : "A very very simple StepFunction",
  "StartAt": "Hello", 
  "States": {
    "Hello": {
      "Type": "Wait",
      "Seconds": 10,
      "Next": "World"
    },
    "World": {
      "Type": "Pass",
      "End": true
    }
  }
}

If you are not familiar with Stepfunction, AWS Stepfunction console has a nice visualization

stepfunction image


Next we need to upload the helloworld.asl.json to s3 bucket. We also need to define a mapping to access this json file.


Our output will still be the same since our mapping is lazy. Next, we will add stepfunction to our resources

The final cloudformation template is

The cloudformation template is generated under

# for typescript
cdk.out\TestCdkStack.template.json
# for python 
cdk.out\test_cdk.template.json

Or you can simply deploy it using

$ cdk deploy

Footnotes


  1. Github issues


comments powered by Disqus