Sub topic 9
Bootstrapping an environment
This chapter assumes you can create a CDK project and run cdk synth, and that you have AWS credentials configured for the account you want to deploy to. An environment is one account and region pair, as covered in the previous chapter.
What bootstrapping does
Some stacks need resources before CloudFormation can start. A Lambda function's code has to sit in S3 first, and a container image has to sit in a registry. Bootstrapping creates those resources once per environment, so every CDK app deployed there can use them.
The command deploys a CloudFormation stack named CDKToolkit. It contains:
- An S3 bucket that stores file assets such as Lambda code.
- An Amazon ECR repository that stores Docker image assets.
- IAM roles for publishing files, publishing images, looking up context values, and deploying.
- A CloudFormation execution role, which CloudFormation assumes to create your resources.
- An SSM parameter that records the bootstrap version.
Running the command
Bootstrap each environment once. Name the account and region explicitly so you never bootstrap the wrong target.
cdk bootstrap aws://123456789012/us-east-1
cdk bootstrap aws://123456789012/eu-west-1Run it again whenever a new CDK release requires a newer bootstrap version. The command updates the existing stack and is safe to repeat.
If you run cdk bootstrap with no argument, the CLI uses the account and region of your current credentials. In a shared setup, name the target instead.
Common options
| Option | Purpose |
|---|---|
| `--cloudformation-execution-policies` | Sets the managed policies attached to the execution role. The default is `AdministratorAccess`. |
| `--trust` | Lets another account deploy into this one. Pipelines use it. |
| `--qualifier` | Lets several bootstrap stacks coexist in one environment. |
| `--profile` | Chooses the AWS CLI profile to use. |
The default execution role is broad. In production accounts, pass a narrower policy list so the role can only create the resource types your apps need.
Common errors
- "This stack uses assets, so the toolkit stack must be deployed": the environment is not bootstrapped. Run
cdk bootstrapfor the exact account and region in the error. - "Bootstrap stack version is too old": the stack predates what your CDK version expects. Run
cdk bootstrapagain. - Access denied during bootstrap: your credentials need permission to create IAM roles, buckets and repositories in that account.
- Deploying to a region you never bootstrapped: bootstrapping is per region. A bootstrapped
us-east-1does not covereu-west-1.
Exercise
You deployed a stack with a Lambda asset to us-east-1 successfully. A teammate changes the stack's region to eu-west-1 and deploys, but the deploy fails with a message that the toolkit stack must be deployed. What is the cause and the fix?
Show answer
Bootstrapping is per account and region. eu-west-1 has no CDKToolkit stack, so no asset bucket or deploy roles exist there. Run cdk bootstrap aws://ACCOUNT/eu-west-1 with the real account ID, then deploy again.