CDK

What is the CDK?

AWS Cloud Development Kit, or CDK, is a tool that simplifies the creation of CloudFormation as well as has patterns to help do common tasks in AWS easily and in line with best practices.

You write your intentions in actual programming languages such as Typescript, Python, C# and Java and it ‘compiles’ this into CloudFormation on your behalf.

cdk

So why is it relevant here? Because it can let you do everything you’ve done so far with the CLI and no clicking around the console.

Example CDK code to represent everything you’ve done so far

In addition to our sample .NET application in awscloudbuilders-containerise-dotnet/aspnetapp there is an example of doing everything we’ve done so far with CDK in the Cdk folder in that same repo.

Have a look at awscloudbuilders-containerise-dotnet/Cdk/CdkStack.cs:

using Amazon.CDK;
using Amazon.CDK.AWS.EC2;
using Amazon.CDK.AWS.ECS;
using Amazon.CDK.AWS.ECS.Patterns;

namespace Cdk
{
    public class CdkStack : Stack
    {
        internal CdkStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props)
        {
            var vpc = new Vpc(this, "MyVpc", new VpcProps
            {
                MaxAzs = 2 // Default is all AZs in region
            });

            var cluster = new Cluster(this, "MyCluster", new ClusterProps
            {
                Vpc = vpc
            });

            // Create a load-balanced Fargate service and make it public
            new ApplicationLoadBalancedFargateService(this, "MyFargateService",
                new ApplicationLoadBalancedFargateServiceProps
                {
                    Cluster = cluster,          // Required
                    DesiredCount = 2,           // Default is 1
                    TaskImageOptions = new ApplicationLoadBalancedTaskImageOptions
                    {
                        Image = ContainerImage.FromAsset(".")
                    },
                    MemoryLimitMiB = 1024,      // Default is 256
                    PublicLoadBalancer = true    // Default is false
                }
            );
        }
    }
}

With this few lines of code (in this case .NET code - but it could be TypeScript, Python or Java as well etc.) we are telling CDK that we want to:

  1. Create a VPC
  2. Create an ECS Cluster for Fargate in that VPC
  3. Create an ECR repository
  4. Do a local docker build on the machine running CDK and then push the resulting image to that new ECR repository
  5. Create an ECS Task Definition for that image
  6. Create an ALB
  7. Create an ECS Service to manage that ALB running that new ECS task definition behind it

You can learn more about the CDK code used here in our reference documentation: https://docs.aws.amazon.com/cdk/api/latest/docs/aws-ec2-readme.html and https://docs.aws.amazon.com/cdk/api/latest/docs/aws-ecs-patterns-readme.html.

Let’s see it in action!

Open a Terminal in your Cloud9 and type the following commands.

First we need to install the CDK

npm install -g cdk

Then we’ll install the .NET Core SDK

cd ~/environment
mkdir dotnet
wget https://download.visualstudio.microsoft.com/download/pr/f65a8eb0-4537-4e69-8ff3-1a80a80d9341/cc0ca9ff8b9634f3d9780ec5915c1c66/dotnet-sdk-3.1.201-linux-x64.tar.gz
cd dotnet
tar -zvxf ../dotnet-sdk-3.1.201-linux-x64.tar.gz
sudo mv ~/environment/dotnet /usr/share
sudo ln -s /usr/share/dotnet/dotnet /usr/bin/dotnet
dotnet --version

Then ensure that you’ve cd’d back to the project folder

cd ~/environment/awscloudbuilders-containerise-dotnet

Then we’ll run the cdk synth command to get a sense of all the wonderful CloudFormation that CDK is generating for us - 880 lines of it!

cdk synth

Now lets deploy this to our account:

Set the region we want to use by running

export CDK_DEFAULT_REGION=ap-southeast-2

We need to bootstrap CDK in this account (basically create an S3 bucket for it to put its artefacts in) by running

cdk boostrap

Finally we’ll tell CDK to deploy this for us by running

cdk deploy

It’ll show you some of the securty-related changes it will make if you proceed - enter ‘y’ and hit Enter