Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
November 16, 2021 11:00 pm GMT

How to Rename an AWS S3 Bucket in Terraform

Occasionally you might want to rename an AWS S3 bucket you are managing with Terraform. However, names of S3 buckets are immutable, which means you cant change them directly. If you tried, Terraform would destroy the old one and then create a new one, resulting in data loss.

To avoid this, you need to create a new bucket with the desired name, move the data over to it, make the relevant Terraform state replacements, and finally delete the old bucket.

In this post you will learn how to rename an AWS S3 bucket in Terraform. First things first lets say you have a bucket definition in your Terraform code:

resource aws_s3_bucket my_bucket {    bucket = old-name}

and you want to change the name of the bucket to new-name.

Step 1 - Create the new bucket

Firstly, well need to create a new bucket. You can do this using the AWS CLI or the AWS console. Just make sure to properly replicate the old settings, especially the ACL (so that your data doesnt accidentally become public).

Now, we can copy all the files from the old to the new bucket:

aws s3 sync s3://old-name s3://new-name

Step 2 - Modify the State

Now that we have our new bucket, we need to remove the old one from our Terraform state and import the new one in its place:

terraform state rm aws_s3_bucket.my_bucketterraform import aws_s3_bucket.my_bucket new-name

If you tried to run Terraform now, it would show you that theres driftand yes, there would be! Weve just imported a bucket into a resource which still has old-name in the config.

Step 3 - Change the Code

Thats why we now have to finally change the name of the bucket in our Terraform config:

resource aws_s3_bucket my_bucket {    bucket = new-name}

If you run Terraform now, youll see that there are no changes to be made.

Step 4 - Do a Cleanup

If you want to, you can now delete your old bucket using:

aws s3 rm s3://old-name --recursiveaws s3 rb s3://old-name

Make sure all the data has successfully been copied over to the new bucket.

Step 5 - Run the Above Arbitrary AWS CLI Commands if Youre Using Spacelift

If youre using Spacelift then you can use tasks to run the above arbitrary AWS CLI commands. The default runner image already contains the AWS CLI, so no changes are necessary there.

One additional thing you can do is lock the Stack while youre running the migration, this way nobody will accidentally run Terraform or make any other changes while youre moving the data.

And thats it! If you have any questions about how to rename an AWS S3 bucket, drop me a line in the comments and Ill get back to you straight away.

You will find more Terraform Tutorials on our website:


Original Link: https://dev.to/spacelift/how-to-rename-an-aws-s3-bucket-in-terraform-step-by-step-3cb4

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To