I was working on some changes to some CDK code in my AWS account and I made a typo (lack of sleep does some crazy things) in the bucket name (the correct name is something like mybucket-alpha
) so it created a new bucket (mybucket-alpha-alpha
). I rolled back the change but now I get a "Bucket with name mybucket-alpha
already exists" error when I deploy. It seems that the old mybucket-alpha
is no longer in the part of my CDK stack, so I get this error because CDK is trying to recreate the bucket. Is there a quick way to reimport this bucket into my stack? I can’t delete it since it contains a ton of important data
Question posted in Amazon Web Sevices
The official Amazon Web Services documentation can be found here.
The official Amazon Web Services documentation can be found here.
2
Answers
there is a way to import existing resources into you CDK Stack.
In case of S3 Bucket you can use the Bucket.fromBucketArn() method. You can find it in the documentation as well: https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_s3.Bucket.html#static-fromwbrbucketwbrarnscope-id-bucketarn
Quick example:
Here is a thorough explanation of how to import an existing bucket into a cdk stack: https://medium.com/@visya/how-to-import-existing-aws-resources-into-cdk-stack-f1cea491e9
In case the link breaks, the basic steps are…
cdk synth
to generate a template.After importing the bucket into the stack you can managed it via you cdk app/stack.