>Getting started with AWS CDK

AWS CDK is an open-source software development framework that allows you to define your cloud application resources using familiar programming languages, like JavaScript, TypeScript, Python, Java, C#, and Go.

  • To use CDK, you need to have a few prerequisites installed and configured. 

One of the benefits for AWS CDK is that you can leverage your favorite development environment and have a rich experience when exploring the hundreds of different services and features of AWS. I will be using Virtual Studio Code (VSCode) but there are many IDEs that support code-completion and syntax highlighting for your language of choice:

  • VSCode
  • AWS Cloud9
  • Atom
  • emacs
  • PyCharm

Before you start your work with the AWS CDK, you need to have its toolkit (CLI) ready and available on your workstation. This tool will be the link between the code you write and the infrastructure you provision, so it is important to have it set up and using the latest version. Additionally, once the CLI is configured, you will need to bootstrap your account so CDK can deploy resources to it properly. The bootstrapping process creates resources required for the CDK to operate correctly.

To install the AWS CDK CLI, you need to first have the Node Package Manager (npm) installed. This can be found at the website https://nodejs.org/

  • Verify you have node.js installed by typing in the command:
node --version

Install the AWS CDK CLI globally by running the following command:

npm install -g aws-cdk

To verify that it has been successfully installed run the following command:

cdk --version

Bootstrapping your AWS Account

Many AWS CDK stacks that you will deploy, will include assets, external files that are deployed with the stack, such as AWS Lambda functions or Docker images. The CDK uploads these to an Amazon S3 bucket or other containers so they are available to AWS CloudFormation during deployment. Deployment requires that these containers already exist in the AWS account and the region you are deploying into. Creating these containers is called bootstrapping.   *From the AWS website

To bootstrap your account type:

# Get the account ID
aws sts get-caller-identity

# Bootstrap the account
cdk bootstrap aws://ACCOUNT-NUMBER/REGION

Creating infrastructure

Using the AWS CDK CLI to create a new infrastructure project using TypeScript. Learn how to write a simple resource, and how you can synthesize and deploy your CDK code. Synthesizing is how CDK turns your infrastructure code into AWS CloudFormation templates.

To create a new CDK project, I will be using the CDK CLI.

To get started, create an empty directory on your system and change into it.

mkdir cdk-demo

cd cdk-demo

cdk init --language typescript

This will create the folder structure and install some of the necessary modules required for a TypeScript CDK project. It also provides you with commands that are needed to help you get started with your CDK project. I would take note of these in notepad or somewhere you can remember.

To start building out a project, a common starting point is to create a logically isolated virtual network that you define, called an Amazon Virtual Private Cloud (VPC). Before creating your first VPC, it is important to understand the files that the ‘cdk init’ command created.

Here are some of the important files and what they are used for:

  • bin/cdk-project.ts – This is the entry point to your CDK application. This will load/create all the stacks we define under lib/*
  • lib/cdk-project-stack.ts – This is where your main CDK application stack is defined. Your resources and its properties can go here.
  • package.json – This is where you define your project dependencies, as well as some additional information and build scripts (npm build, npm test, npm watch).
  • cdk.json – This file tells the toolkit how to run your application as well as some additional settings and parameters related to CDK and your project.

*From the AWS website

Before we define our VPC in the main stack, we need to ensure we deploy to the correct account and region. While the CDK will pull this information from the local ‘aws cli’ configuration, it is best to configure this manually in your CDK code to avoid incorrect values when that config changes.

I modified the ‘bin/cdk-demo.ts’ to look like this. You would use your account ID and preferred region:

#!/usr/bin/env node
import 'source-map-support/register';
import * as cdk from '@aws-cdk/core';
import { CdkDemoStack } from '../lib/cdk-demo-stack';

const app = new cdk.App();
new CdkDemoStack(app, 'CdkDemoStack', {
   env: { account: '123456789012', region: 'eu-west-1' },


Code for the VPC

Install the Amazon EC2 module using npm by running the following command in the project directory:

npm install @aws-cdk/aws-ec2

Now, I can create the VPC. When opening the stack definition in ‘lib/cdk-demo.ts’ it should look something like this:

import * as cdk from '@aws-cdk/core';
export class CdkDemoStack extends cdk.Stack {
   constructor(scope: cdk.Construct, id: string, props?:cdk.StackProps) {
      super(scope, id, props);

   // The code that defines your stack goes here

This is the skeleton of the project. To get started with the VPC, I need to import the installed module and reference the code modules inside them. To do this, I will be importing the ‘Vpc’ and the ‘SubnetType’ class:

import * as cdk from '@aws-cdk/core';
   // CHANGE: This is where you import the classes from the module:
import { Vpc, SubnetType } from '@aws-cdk/aws-ec2';
export class CdkDemoStack extends cdk.Stack {
   constructor(scope: cdk.Construct, id: string, props?:cdk.StackProps) {
      super(scope, id, props);

   // The code that defines your stack goes here   

When creating a VPC, there are a number of properties we can set to tailor it to our needs. By default, it will create a VPC across 3 availability zones (AZs), with public and private subnets (with a single Internet Gateway and 3 NAT Gateways). In this practice, I was only to create a very simple setup spanning 2 AZ’s, and with a public subnet for each.

To create the VPC, I will specify the two AZ’s and the details to create a public subnet:

import * as cdk from '@aws-cdk/core';
import { Vpc, SubnetType } from '@aws-cdk/aws-ec2';

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

// The code that defines your stack goes here
// CHANGE: We have created the vpc object from the Vpc class.

      const vpc = new Vpc(this, 'MainVpc',{

// CHANGE: this is where we define how many AZs to use   
      maxAzs: 2,

// CHANGE: We define a single subnet configuration per AZ.

// CHANGE: this is it's CIDR mask so
              cidrMask: 24,

// CHANGE: a name for each of these subnets
              name: 'public-subnet',

// CHANGE: and the subnet type to be used - here we will have
// a public subnet. There are other options available here.
              subnetType: SubnetType.PUBLIC

I am now ready to deploy this infrastructure change to my account.

Before doing this, I will see if my code is valid by typing ‘npm run build’. If that is successful, I can run the deploy command, ‘cdk deploy’.

After a few minutes, you should get a green check mark followed by an ARN (Amazon Resource Name) of your newly created CloudFormation stack. The new VPC has now been deployed and is ready to be used!

The VPC created in this guide will not cost anything per month, but there is a quota that limits each region in an account to only allow 5 VPCs – you can increase this limit if you need to via a support ticket. If you wish to remove your newly created resources, you can run the ‘cdk destroy’ command and it will remove all the resources via the CloudFormation stack it created earlier.

-All practices and information provided by the AWS website https://aws.amazon.com/getting-started/

Leave a Comment