How to use the existing S3 bucket for the CDK bootstrap?

Piotr Zimoch
cloudaemons
Published in
2 min readJun 4, 2020
Photo by Courtney Moore on Unsplash

If you’re reading this post, probably you have been tasked to check how to utilize the existing bucket for the assets that AWS CDK creates?

TL;DR

Instead of running cdk bootstrap command, get details of your bucket that you are willing to use, and deploy new stack with the following CloudFormation template in the desired region:

Description: >-
The CDK Toolkit Stack. It is mandatory stack, for CDK based apps to work. It
was created by manually and manages resources necessary for managing your
Cloud Applications with AWS CDK.
Parameters:
StagingBucketName:
Type: String
StagingBucketDomainName:
Type: String
Resources:
CDKMetadata:
Type: 'AWS::CDK::Metadata'
Properties:
Modules: aws-cdk=1.42.0
Outputs:
BucketName:
Description: The name of the S3 bucket owned by the CDK toolkit stack
Value:
Ref: StagingBucketName
BucketDomainName:
Description: The domain name of the S3 bucket owned by the CDK toolkit stack
Value:
Ref: StagingBucketDomainName

Ok, once we know how to do it, let me tell you when you might it.

In different organizations, there are different rules. CDK is flexible enough to create an infrastructure that will satisfy all the governance rules. BUT, the only not flexible part that I consider problematic, is the cdk bootstrap and the way how it is creating a bucket.

Bucket created by the bootstrap command is used to store assets generated by the CDK in the cloud, so they are easily accessible by other AWS Services.

For some reason, you can’t import an existing bucket. I imagine that would be a single parameter to add to the CLI, but... it’s not there yet.

Q: When would you need to have a predefined bucket?

A: When the rules that you’re following requires you:

  • To have your bucket named in a predefined way.
  • To add custom tags.
  • You have simply no access to create S3 buckets.

To understand how this works, you have to realize, that cdk bootstrap effectively creates a stack named “CDKTookit”, that has two outputs. That’s it. If you need, you can modify provided templated and add whatever resource you might. You have to only follow only 3 rules:

  1. Keep the stack named CDKToolkit

2. Output BucketName

3. Output BucketDomainName

Thanks for reading, did you find any other limitation in the CDK?

--

--