Sub topic 10
Bootstrapping an Environment
This chapter assumes you can write a basic CDK stack, know what an AWS account and region are, and have the CDK CLI installed with working AWS credentials. It covers what bootstrapping creates, why deployments need it, how to run it, and the errors people commonly hit.
Why bootstrapping exists
A CDK app often contains things that CloudFormation cannot hold inside a template. A Lambda function's code is a zip file. A Docker image is a container image. Large templates exceed the size limit for inline upload. The CDK must put these files somewhere before CloudFormation can reference them.
Bootstrapping provisions that "somewhere". It deploys a CloudFormation stack, named CDKToolkit by default, into one account and region. That stack holds the resources the CDK CLI needs during cdk deploy.
You bootstrap each environment once. An environment is one account and region pair, so aws://123456789012/us-east-1 and aws://123456789012/eu-west-1 are separate environments and each needs its own bootstrap.
What the bootstrap stack contains
The modern bootstrap template creates these resources:
| Resource | Purpose |
|---|---|
| S3 bucket | Stores file assets such as Lambda code zips and large templates |
| ECR repository | Stores Docker image assets |
| IAM deployment role | Assumed by the CLI to call CloudFormation |
| IAM file-publishing role | Lets the CLI upload to the bucket |
| IAM image-publishing role | Lets the CLI push to the repository |
| IAM lookup role | Lets the CLI read account details for context lookups |
| CloudFormation execution role | Passed to CloudFormation, which uses it to create your resources |
| SSM parameter | Records the bootstrap template version |
The CLI does not use your own credentials to create resources directly. It uses your credentials to assume the bootstrap roles, and CloudFormation then acts through the execution role. This split lets you give a person or pipeline permission to deploy without giving them broad rights over everything the stack creates.
Running the bootstrap
Name the environment explicitly the first time:
cdk bootstrap aws://123456789012/us-east-1If your credentials and default region are configured, you can run cdk bootstrap with no argument and the CLI uses the current identity and region. Explicit targets are safer, because a stale default profile is a common way to bootstrap the wrong account.
You can bootstrap several environments in one command:
cdk bootstrap aws://111111111111/us-east-1 aws://222222222222/eu-west-1Running the command again is safe. It updates the stack to the newest template and leaves existing assets alone. Rerun it after upgrading the CDK CLI if a deployment reports that the bootstrap version is too old.
Bootstrapping and your app's environment
A stack that sets env with a concrete account and region deploys to exactly that environment, which must be bootstrapped. A stack with no env is environment-agnostic. The CLI then fills in the account and region from your current credentials at deploy time.
The topic spine's stack uses CDK_DEFAULT_ACCOUNT and CDK_DEFAULT_REGION, which the CLI sets from your active profile:
new AppStack(app, 'AppStack', {
env: {
account: process.env.CDK_DEFAULT_ACCOUNT,
region: process.env.CDK_DEFAULT_REGION,
},
});This ties deployment to whichever profile is active. If that profile points at an account you have not bootstrapped, deployment fails before anything is created.
Customizing the bootstrap
The default template suits most solo and small-team use. Several flags change it:
--cloudformation-execution-policiessets the managed policies attached to the execution role. The default isAdministratorAccess. Replace it with a narrower policy for tighter control.--trustlists account IDs allowed to deploy into this environment. You need this for cross-account pipelines.--toolkit-stack-nameand--qualifierchange the stack name and the resource-name suffix. Use these when two independent CDK setups must coexist in one account.--bootstrap-customer-keyadds a KMS key for encrypting bucket contents.
A cross-account setup, where a pipeline account deploys into a workload account, looks like this:
cdk bootstrap aws://222222222222/us-east-1 \
--trust 111111111111 \
--cloudformation-execution-policies arn:aws:iam::aws:policy/AdministratorAccessRun it with credentials for the workload account (222222222222). The --trust value is the pipeline account (111111111111).
If you change the qualifier at bootstrap time, your app must use the same one. Set it with the @aws-cdk/core:bootstrapQualifier context key in cdk.json, or through a custom stack synthesizer. A mismatch produces errors about missing roles or buckets.
Edge cases and common errors
Missing bootstrap. The message This stack uses assets, so the toolkit stack must be deployed to the environment means the target is not bootstrapped. Run cdk bootstrap for that account and region.
Outdated bootstrap. A message that the stack requires bootstrap version N or later means the template is old. Rerun cdk bootstrap with a current CLI.
Wrong account. Bootstrapping uses whatever credentials the CLI finds. Check with aws sts get-caller-identity before you run it.
Deleted bootstrap stack with retained resources. The asset bucket and repository are retained when you delete the CDKToolkit stack. A later bootstrap can fail because those names already exist. Remove the leftovers manually, but only after confirming no deployed stack still depends on them.
Cost. Bootstrapping itself is nearly free, but assets accumulate. The bucket keeps every uploaded version of your code, so old assets cost storage. Consider a lifecycle rule or periodic cleanup.
Checking what is bootstrapped
Open the CloudFormation console in the target region and look for CDKToolkit. Its Outputs tab shows the bucket name and the bootstrap version. You can also read the version parameter with the CLI:
aws ssm get-parameter \
--name /cdk-bootstrap/hnb659fds/version \
--region us-east-1The hnb659fds segment is the default qualifier. If you used --qualifier, put your value there.
Exercise
A team deploys a CDK app from a pipeline account (111111111111) into a production account (222222222222). The first deploy fails with a message that the deployment role cannot be assumed. Which account should you bootstrap, with which flag, and why?
Show answer
Bootstrap the production account (222222222222), using credentials for that account, and add --trust 111111111111. The bootstrap roles in the production account must list the pipeline account as a trusted principal before the pipeline can assume them. Bootstrapping only the pipeline account does not create the roles the deploy needs in production.
Exercise
You run cdk bootstrap --qualifier myteam. Your next cdk deploy fails because it cannot find the file-publishing role. What is the likely cause and the fix?
Show answer
The app still uses the default qualifier hnb659fds, so it looks for roles and buckets with that name. Set the @aws-cdk/core:bootstrapQualifier context value to myteam in cdk.json, or configure the stack synthesizer with the same qualifier, then deploy again.