AWS CDK S3 Bucket Creation Error - Bucket_Name already exisits

Viewed 5106

I am new to using CloudFormation / CDK and am having trouble figuring out to deploy my stacks without error. Currently I am using the python CDK to create a bucket. This bucket will hold model files and I need to ensure that the bucket deployed in this stack retains data over time / new deployments. From my initial tests, it seems that if bucket_name is not specified, the CDK will randomly generate a new bucket name on deployment, which is not ideal.

Here is the snippet used to create the bucket:

bucket = aws_s3.Bucket(self, "smartsearch-bucket", bucket_name= 'mybucketname')

The first time I run cdk deploy, there are no problems and the bucket is created. The second time I run cdk deploy, I get an error stating that my S3 bucket already exists. What else is needed so that I can redeploy my stack using a predetermined S3 bucket name?

2 Answers

I ran into the same issue, and it was due to the reason that bucket was already created by me manually earlier for some testing, NOT by ECS stack initially. Deleting the bucket definitely makes ECS deployment to work fine, as it did for you and I tested this running the deployment multiple times. Ensure that no ECS resources are pre-created manually.

The way ECS would identify if it has to re-create a resource is via these tags:

enter image description here

The S3 bucket names are global, it means that if someone else on a different account has chosen this name, it wont work, you should provide a very unique name for your bucket.

Related