Bootstrapping a new CDK project – Programmatic Approach to IaC with AWS CDK

The very first step before we can work with AWS CDK is to bootstrap an empty directory with one of the supported programming languages. To make this task easier for users, CDK offers a command-line interface utility that is pre-installed for you in the Cloud9 IDE that we have been using throughout this book.

Note

The steps in this section have already been executed for you, inside the chapter-6/ directory, so you may just skim through them for understanding.

Let’s start a new session on our Cloud9 IDE and switch to the chapter-6/ directory in the terminal, before starting the CDK project initialization process:

aws-devops-simplified:~/environment/chapter-6 $ npx aws-cdk init –language typescript

npx: installed 1 in 8.495s

Applying project template app for typescript

# Welcome to your CDK TypeScript project

This is a blank project for CDK development with TypeScript.

The `cdk.json` file tells the CDK Toolkit how to execute your app. ## Useful commands

  • `npm run build`   compile typescript to js
  • `npm run watch`   watch for changes and compile
  • `npm run test`perform the jest unit tests
  • `cdk deploy` deploy this stack to your default AWS account/ region
  • `cdk diff`compare deployed stack with current state
  • `cdk synth` emits the synthesized CloudFormation template Initializing a new git repository…

cdk init uses the name of the directory to generate dynamic prefixes, or suffixes, for most things – classes, subfolders, and so on.

Tip

You might have noticed that instead of using the cdk command line directly, we used npx aws-cdk instead. This practice allows you to pin these components to specific versions andensure that every developer uses the same versions. Generally speaking, new releases of these components are always backward compatible, so the chance of something breaking is rare.

Once the bootstrapping is complete, you will observe the following hierarchy of folders and files in your project directory. I have added some descriptions highlighting the significance of each of these:

aws-devops-simplified:~/environment/chapter-6 $ tree -L 1 .

.

├── README.md

├── bin                         >> (Application entry point)

├── cdk.json              >> (CDK project configurations)

Having bootstrapped the code directory that will host our application code, let’s move on to preparing our AWS account so that CDK can use it to deploy resources.

Bootstrapping the AWS account to enable CDK deployments

AWS CDK deployments are targeted in a specific region of an account. Before you go ahead and deploy some resources, you need to bootstrap the target environment. As part of the bootstrapping process, CDK creates resources such as S3 buckets, IAM roles/policies, ECR repositories, and SSM parameters. These resources are later used by the CDK toolkit to orchestrate deployment activities in your account. Before proceeding with the cdk bootstrap command, we need to run npm install as a prerequisite so that all Node.js modules can be fetched. Bootstrapping is as simple asrunning the following commands:

aws-devops-simplified:~/environment/chapter-6 $ npm install npm WARN old lockfile

npm WARN old lockfile This is a one-time fix-up, please be patient…

npm WARN old lockfile

added 312 packages, and audited 331 packages in 20s

aws-devops-simplified:~/environment/chapter-6 $ cdk bootstrap

At this point, both the AWS account and local code repository have been prepared for deployment.

Now is a good time to add concrete resources that will make up our application.

Leave a Comment