Setting up the AWS Cloud Development Kit (CDK)

·

3 min read

Setting up the AWS Cloud Development Kit (CDK)

This is a brief explanation on how to set up the AWS Cloud Development Kit (CDK) so you can start building your serverless applications.

It's a straightforward 3-step process; you need to install and configure NodeJS, the AWS CLI, and finally the AWS CDK.

Installing NodeJS

You will need nodejs on your machine. There are a number of ways to install node but my preference is with Node Version Manager (nvm) because it makes the installation of multiple node versions considerably easier in the future.

For MacOs you can install nvm with Homebrew:

$ brew install nvm

The setup for nvm is straightforward and you can just follow the instructions once the brew installation is complete.

mkdir ~/.nvm

Add the following to ~/.zshrc or your desired shell configuration file:

  export NVM_DIR="$HOME/.nvm"
  [ -s "/usr/local/opt/nvm/nvm.sh" ] && \. "/usr/local/opt/nvm/nvm.sh"  # This loads nvm
  [ -s "/usr/local/opt/nvm/etc/bash_completion.d/nvm" ] && \. "/usr/local/opt/nvm/etc/bash_completion.d/nvm"  # This loads nvm bash_completion

Now that you have nvm installed, we can install NodeJS as required by CDK. I'd recommend a stable long term support version of node, and the easiest way to install it is with the --lts flag to nvm install.

$ nvm install --lts

Once this is finished, verify your node installation by checking the version.

$ node -v
v16.15.1

Installing AWS CLI

If you've worked with AWS previously you probably have aws-cli installed already. If not, you can install it with Homebrew:

$ brew install aws-cli

To configure aws-cli you will need your access key and secret access key, you can get these from the AWS Console.

$ aws configure
AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: eu-west-1
Default output format [None]: json

Installing CDK

Use npm to install the aws-cdk package globally so you can use the cli from your terminal.

$ npm install --location=global aws-cdk

Once installed you should be able to use the CLI and check the version.

$ cdk --version
2.31.0 (build b67950d)

Before using the CDK to create infrastructure in your AWS account, the account needs to be bootstrapped by CDK. This will create a CDKToolkit CloudFormation stack in the account with the basic resources needed for future application deployments.

You'll need to know your AWS account number and the region you intend to deploy into. You can get this information from the AWS console of course, but the quick way is to use the AWS CLI.

$ aws sts get-caller-identity
{
    "UserId": "0123456789ABCDEF01234",
    "Account": "01234567890123",
    "Arn": "arn:aws:iam::01234567890123:user/cdk"
}

You can then bootstrap your account with cdk bootstrap like so:

$ cdk bootstrap aws://01234567890123/eu-west-1

Once bootstrapped you can initialise your first CDK app. Create an empty directory and run cdk init app with the --language flag set to your language of choice.

$ mkdir my-cdk-app
$ cd my-cdk-app
$ cdk init app --language=javascript
Applying project template app for javascript
# Welcome to your CDK JavaScript project

This is a blank project for CDK development with JavaScript.

The `cdk.json` file tells the CDK Toolkit how to execute your app. The build step is not required when using JavaScript.

## Useful commands

* `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...
Executing npm install...
✅ All done!

That's it! CDK has created the boilerplate for you and initialised a new git repository so you are ready to go.