A beginner's guide to CDK, by building an ECS cluster

A beginner's guide to CDK, by building an ECS cluster

Over the past couple of months, AWS CDK has come a long way. We even saw Hashicorp a major player in the IaC game with their open-source tool Terraform . A tool for provisioning, changing, and versioning infrastructure safely and efficiently, integrate CDK to their building tool which natively supported HCL and JSON only. This meant support for programming languages such as Python and TypeScript with support for Javascript, Java, and C# likely to be available in the future.

Well, what is CDK? According to AWS documentation, it is AWS Cloud Development Kit (AWS CDK) an open-source software development framework to model and provision your cloud application resources using your programming language of choice. It is built with TypeScript hence you might find examples in this language ubiquitous. However other programming languages are fully supported. It utilizes constructs basic building blocks for AWS CDK apps. A construct represents a "cloud component" and encapsulates everything AWS CloudFormation needs to create the component.

Prior to this, I had not used CDK so I decided to try it out by following up on a few tutorials, however, this turned out to be muddy waters and motivated me to do a quick write up that is a beginner-friendly guide "I hope".To save you going through the pain of it yourself. Let's dive in by building a cluster in Python you can find the code here

My Foolish assumptions, not prerequisites :

  1. You have at least used one IaC tool to provision resources in the cloud
  2. You have your AWS CLI setup
  3. A bit of familiarity with at least one programming language.

Installation.

~$ npm i -g aws-cdk

Installing or updating AWS CDK CLI with npm (requires Node.js ≥ 10.3.0):

You probably wondering why we're installing npm when I said we'll be building with python, this is because AWS Construct Library( mentioned above) is developed in TypeScript and run on Node.js thus we require npm.

If the installation was successful, run

~$ cdk --version (Sample Output)1.59.0 (build 1d082f4)

Next, up let's create an empty folder and cd/ to it you name your folder anything that you want I named mine cdk-demo.

~$mkdir cdk-demo && cd cdk-demo

Create 3 files in this folder...

~/cdk-demo$ touch app.py

~/cdk-demo$ touch cdk.json

~/cdk-demo$ touch requirements.txt

Screenshot from 2020-08-20 12-42-25.png

app.py is where our code is going to sit.

cdk.json file tells the CDK Toolkit how to execute your app

requirements.txt This file is used by pip to install all of the dependencies for your application, in some cases, you'll find it contains only -e. This tells pip to install the requirements specified in setup.py, this is not this case with this setup as it a simplified

Create a python virtual environment and activate it.

~/cdk-demo$ python3 -m venv .env

~/cdk-demo$ source ./.venv/bin/activate

We need to install requirements.txt and fr this we run, I'm using pip3 hence the 3 after pip, take note of which version of pip you're using

~/cdk-demo$ pip3 install -r requirements.txt

I am going to manually install the resources necessary for this demo.

~$ pip3 install aws_cdk.aws_autoscaling

~$ pip3 install aws_cdk.aws_ec2

~$ pip3 install aws_cdk.aws_ecs

Now the code

app.py

from aws_cdk import (
    aws_autoscaling as autoscaling,
    aws_ec2 as ec2,
    aws_ecs as ecs,
    core,
)


class ECSCluster(core.Stack):

    def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, *kwargs)

        vpc = ec2.Vpc(
            self, "ClusterVpc",
            max_azs=2
        )

        asg = autoscaling.AutoScalingGroup(
            self, "ClusterFleet",
            instance_type=ec2.InstanceType("t2.xlarge"),
            machine_image=ecs.EcsOptimizedAmi(),
            associate_public_ip_address=True,
            update_type=autoscaling.UpdateType.REPLACING_UPDATE,
            desired_capacity=3,
            vpc=vpc,
            vpc_subnets={ 'subnet_type': ec2.SubnetType.PUBLIC },
        )

        cluster = ecs.Cluster(
            self, 'EcsCluster',
            vpc=vpc
        )

        cluster.add_auto_scaling_group(asg)
        cluster.add_capacity("DefaultAutoScalingGroup",
                             instance_type=ec2.InstanceType("t2.micro"))

app = core.App()
ECSCluster(app, "DemoCluster")
app.synth()

cdk.json

{
    "app": "python3 app.py"
}

requirements.txt

aws-cdk.core
aws-cdk.aws_autoscaling
aws-cdk.aws_ec2
aws-cdk.aws_ecs

# Work around for jsii#413
aws-cdk.aws-autoscaling-common

jsii ,hold up what is this ??

jsii allows code in any language to naturally interact with JavaScript classes. It is the technology that enables the AWS Cloud Development Kit to deliver polyglot libraries from a single codebase!

Okay, that's all code you need to deploy the cluster. A few more commands then we deploy.

run

~$ cdk synth

This command synthesizes the CloudFormation template for this code and gives you output on your terminal.

Sample output, but probably longer

Screenshot from 2020-08-20 13-53-09.png

~$ cdk list

To list available stacks, in this example this stack available is called "DemoCluster"

Now let's deploy!!!

~$ cdk deploy

It will likely give you a warning about your AutoScalingGroup and then list all resources to be deployed. See github.com/aws/aws-cdk/issues/5215

And a note that: There may be security-related changes not in this list. See github.com/aws/aws-cdk/issues/1299) Screenshot from 2020-08-20 14-03-30.png

Screenshot from 2020-08-20 14-09-05.png

Press y and the return key to continue......and magic this may take a couple of minutes depending on the number of resources being provisioned and deployed

Screenshot from 2020-08-20 14-15-33.png

If your deployment is a success you should get an output message like this

Screenshot from 2020-08-20 14-24-10.png

If you open your AWS and go to Cloudformation you should be able to see your deployed stack and if you explore resources in your stack you'll find your cluster.

Screenshot from 2020-08-20 14-27-22.png

Screenshot from 2020-08-20 14-29-39.png

Cleaning up

To destroy/remove all the newly created resources, run:

~$ cdk destroy

Screenshot from 2020-08-20 14-35-05.png

That's a wrap, I find CDK and interesting way to provision and deploy cloud resources and I find the adoption to be welcome within the software development community as it eliminates the learning curve for extra language and tools for IaC as you can write and your code and define your infrastructure using the same language.

Some useful cdk commands

  • cdk ls list all stacks in the app
  • cdk synth emits the synthesized CloudFormation template
  • cdk deploy deploy this stack to your default AWS account/region
  • cdk diff compare deployed stack with the current state
  • cdk docs open CDK documentation

You can do further reading here cdkworkshop

And sign up for CDKday, 30th September 2020

Thank you for reading!!!

I hope you find this post useful on how to get started with AWS CDK.

You can find me on Twitter for any follow-up.