AWS Cloud Development Kit

Published at Aug 20, 2023

Before you begin

A lot of fuss is being made about SST and AWS CDK. I have been using CDK for a while now and I am very happy with it. I have not tried SST yet but I am sure it is great too. I am not going to compare the two here. I am just going to show you how to get started with CDK.

About

The AWS Cloud Development Kit (AWS CDK) is an open source software development framework to define your cloud application resources using familiar programming languages. AWS CDK provisions your resources in a safe, repeatable manner through AWS CloudFormation.

Requirements

  • An AWS account
  • Node.js version 10.3.0 or later
  • AWS CLI version 2.0 or later
  • AWS CDK version 1.75.0 or later

Installation

Install the AWS CDK CLI:

npm install -g aws-cdk

Verify the installation:

cdk --version

Bootstrap

Get your AWS account number:

aws sts get-caller-identity

Get your accountโ€™s default region:

aws configure get region

Bootstrap your account:

cdk bootstrap aws://ACCOUNT-NUMBER/REGION

Create a new CDK project

Create a new directory and initialize a new CDK project:

mkdir cdk-demo
cd cdk-demo
cdk init --language typescript

Reviewing the files created:

tree -I node_modules -L 2

.
โ”œโ”€โ”€ bin
โ”‚   โ””โ”€โ”€ cdk-demo.ts
โ”œโ”€โ”€ cdk.json
โ”œโ”€โ”€ jest.config.js
โ”œโ”€โ”€ lib
โ”‚   โ””โ”€โ”€ cdk-demo-stack.ts
โ”œโ”€โ”€ package.json
โ”œโ”€โ”€ package-lock.json
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ test
โ”‚   โ””โ”€โ”€ cdk-demo.test.ts
โ””โ”€โ”€ tsconfig.json

Specifying the environment correctly

Install DefinitelyTyped @types/node:

npm install @types/node

Modify bin/cdk-demo.ts to:

new CdkDemoStack(app, 'dev', { 
  env: { 
    account: process.env.CDK_DEPLOY_ACCOUNT || process.env.CDK_DEFAULT_ACCOUNT, 
    region: process.env.CDK_DEPLOY_REGION || process.env.CDK_DEFAULT_REGION 
}});

Create a script cdk-deploy-to.sh:

#!/usr/bin/env bash
if [[ $# -ge 2 ]]; then
    export CDK_DEPLOY_ACCOUNT=$1
    export CDK_DEPLOY_REGION=$2
    shift; shift
    npx cdk deploy "$@"
    exit $?
else
    echo 1>&2 "Provide account and region as first two args."
    echo 1>&2 "Additional args are passed through to cdk deploy."
    exit 1
fi

Make the script executable:

chmod +x cdk-deploy-to.sh

Deploy to a specific account and region:

./cdk-deploy-to.sh 123456789012 us-east-1

Writing a first resource

npm install @aws-cdk/aws-ec2

Modify lib/cdk-demo-stack.ts to:

import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as ec2 from 'aws-cdk-lib/aws-ec2';

export class CdkDemoStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    new ec2.Vpc(this, 'mainVPC', {
      maxAzs: 2,
      subnetConfiguration: [
        {
         cidrMask: 24,
         name: 'public-subnet',
         subnetType: ec2.SubnetType.PUBLIC,
       }
      ]
   });
  }
}

Deploy the stack:

Use the command below directly to deploy to your default account and region or use the cdk-deploy-to.sh script to deploy to a specific account and region.

cdk deploy

The cdk deploy command compiles your TypeScript into JavaScript and creates a CloudFormation change set to deploy this change.

CDK manages all of this for you, along with uploading the template file to S3 and using CloudFormation to run it. After a few minutes, you should get a green check mark along with an ARN (Amazon Resource Name) of your newly created CloudFormation stack. Your new VPC has now been deployed and is ready to be used.

cdk destroy

Conclusion

Learning AWS CDK is a great way to learn AWS CloudFormation. SST is totally based on CDK and may come with a few limitations. Learning CDK will give you more freedom and power than knowing only SST.

Crafted with โค๏ธ by @kayaman

ma.rco.sh. ยฉ 2023