AWS CDK Bootstrap Error and How To Fix It
I went on a cleaning spree on my account and started removing buckets that I no longer needed from S3. I unknowingly removed a bucket that is used by the AWS CDK toolkit to stage resources before creating the Cloudformation stacks. It was not apparent to me at first that I had created an error that will follow me and lead me down a rabbit hole later.
Later that day I was working on a CDK project and wanted to deploy it to a test account and run some metrics on the deployed solution. I got this error from the console after running the cdk deploy
command:
[100%] fail: No bucket named 'cdktoolkit-stagingbucket-fol1pifxqq1f'. Is account ############ bootstrapped?
❌ CdkWorkshopStack failed: Error: Failed to publish one or more assets. See the error messages above for more information.
at Object.publishAssets (/Users/gerardketuma/Projects/cdk/node_modules/aws-cdk/lib/util/asset-publishing.ts:25:11)
at Object.deployStack (/Users/gerardketuma/Projects/cdk/node_modules/aws-cdk/lib/api/deploy-stack.ts:231:3)
at CdkToolkit.deploy (/Users/gerardketuma/Projects/cdk/node_modules/aws-cdk/lib/cdk-toolkit.ts:180:24)
at initCommandLine (/Users/gerardketuma/Projects/cdk/node_modules/aws-cdk/bin/cdk.ts:201:9)
Failed to publish one or more assets. See the error messages above for more information.
Looking at the first line of the snippet above, the first thing I did was run the bootstrap command again, cdk bootstrap
. This command ran successfully and I thought that the issue was resolved. So I did another deploy and it failed again.
The issue is that when you first use the CDK framework, it creates a stack in Cloudformation called CDKToolkit
and that stack creates a bucket in S3 that is used to stage and hold your resources. Because I had inadvertently removed that bucket ad-hoc, the Cloudformation stack has no way of knowing that the bucket no longer exists. If you drill into the stack and look at the resources tab, you will see the Logical ID called StagingBucket
with a corresponding Physical ID which is the bucket referenced in the first line of the error above.
The fix is to remove and delete the stack called CDKToolkit
and then bootstrap again to get a new bucket created in S3.
The Fix
I’ll document the steps taken to resolve the issue above:
- Login to the AWS Management Console.
- Open the Cloudformation Service.
- Under Stacks, select the
CDKToolkit
stack and click onDelete
. You will get a warning that deleting this stack will delete all the stack resources. However this is not something to worry about because the stack was created by CDK and can be easily recreated. It does not contain any of your own resources. - Re-run the cdk bootstrap command
cdk bootstrap
to re-generate the Cloudformation stack including the staging bucket in S3.
Now when you run cdk deploy
it should work without any issues.