What does CDK Bootstrap do

avatar

Borislav Hadzhiev

Fri Apr 23 20212 min read

banner

Photo by Xiaolong Wong

Updated on Fri Apr 23 2021

CDK bootstrap creates a CloudFormation stack with an S3 bucket, that stores file assets, required for deployments.

What is CDK bootstrap #

CDK bootstrap is a command we can access using the CDK CLI.

shell
npx cdk bootstrap

The cdk bootstrap command provisions a CloudFormation stack called CDKToolkit. This CloudFormation stack is specific to the environment (region and account) our CDK stack is configured for.

Every CDK application belongs to a specific environment.

An environment consists of an account number and a region, where the CDK app is going to be deployed.

We set the environment for a CDK project when instantiating a CDK stack:

const app = new cdk.App();

const myStack = new MyCdkStack(app, 'my-cdk-stack', {
  stackName: `my-cdk-stack`,
  // ๐Ÿ‘‡ set the environment
  env: {
    region: process.env.CDK_DEFAULT_REGION,
    account: process.env.CDK_DEFAULT_ACCOUNT,
  },
});

The initialization process of the environment consists of:

  • provisioning an S3 bucket, where our assets (i.e. Lambda function code, CloudFormation template) will be stored
  • provisioning IAM roles the CDK CLI needs in order to perform a deployment in the environment In order to deploy to multiple environments we have to bootstrap each environment.

The CDKToolkit stack provisions an S3 bucket, that stores the assets(i.e. Lambda code, CloudFormation template, etc), required for a CDK deployment.

cdk toolkit stack

In the S3 bucket we will have an assets "directory":

assets directory

The assets directory is going to contain our zipped file assets:

zipped file assets

For a simple CDK project with a single lambda function, my file assets consist of the index.js handler code for the Lambda.

Using CDK bootstrap #

In order to bootstrap our default account and region we can run the cdk bootstrap command.

shell
npx cdk bootstrap

We only need to use the bootstrap command once for every environment(region and account).

If we use the command more than once, the CDK CLI will check if our CDKToolkit stack has to be updated. If necessary the stack will be updated, if not invoking the bootstrap command does nothing.

We can also specify other environments to bootstrap:

shell
npx cdk bootstrap --profile my-profile

npx cdk bootstrap ACCOUNT_NUMBER/REGION

npx cdk bootstrap 123456789/us-east-1

Conclusion #

The CDK bootstrap command provisions a CloudFormation stack called CDKToolkit.

The stack consists of an S3 bucket that stores file assets (i.e. Lambda function code, CloudFormation templates), required for deployments.

We have to bootstrap each environment (account and region) separately.

Further Reading #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee