How to use CDK Bootstrap with an Existing S3 Bucket
When using the AWS CDK it’s required to ‘bootstrap’ your environment which as of version 1.24 simply creates an S3 bucket to be used for uploading assets that will be used by the generated CloudFormation stacks. But what if you have an existing bucket for assets like these that you want to continue using and not have another bucket to manage? The bootstrap command doesn’t currently provide an option to pass in an existing bucket. What it does provide, however, is an option to pass in a stack name.
The key is that the stack you use to create your bucket needs to have two outputs:
For example, here is a simple CloudFormation yaml template that will create an S3 bucket and has the necessary Outputs included.
AWSTemplateFormatVersion: "2010-09-09" Resources: StagingBucket: Type: AWS::S3::Bucket Outputs: BucketName: Value: !Ref StagingBucket BucketDomainName: Value: !GetAtt StagingBucket.RegionalDomainName
In an existing stack these two Outputs could be added and an update can be applied to the stack.
With the Outputs set you can now pass the stack name to cdk when deploying with
cdk deploy --toolkit-stack-name <YourCfnStackName>
<YourCfnStackName> with the name of your stack from above that creates the S3 bucket. Now the existing bucket will be used rather than creating a brand new bucket.
If the bucket already exists outside of a CloudFormation stack you can use the import functionality of CloudFormation to import the existing bucket into a CloudFormation stack as described here but you won’t be able to add Outputs during the import. What you can do is run bootstrap against the new stack name with the imported resources and that will update the stack with the required Outputs.
cdk bootstrap aws://<account-number>/<region> --toolkit-stack-name <YourCfnStackNameForImportedBucket>
With that set you can then run deploy pointing at this imported stack name.
Comments powered by Talkyard.