DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Implement Amazon S3 Cross-Region Replication With Terraform
  • Streamlining HashiCorp Cloud Platform (HCP) Deployments With Terraform
  • Automating AWS Infrastructure Testing With Terratest
  • Setting Up a ScyllaDB Cluster on AWS Using Terraform

Trending

  • Can You Run a MariaDB Cluster on a $150 Kubernetes Lab? I Gave It a Shot
  • Navigating and Modernizing Legacy Codebases: A Developer's Guide to AI-Assisted Code Understanding
  • Introducing Graph Concepts in Java With Eclipse JNoSQL
  • Enhancing Business Decision-Making Through Advanced Data Visualization Techniques
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. How to Rename an AWS S3 Bucket in Terraform

How to Rename an AWS S3 Bucket in Terraform

In this post you will learn how to rename an AWS S3 bucket in Terraform.

By 
Jacob Martin user avatar
Jacob Martin
·
Feb. 06, 22 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
9.4K Views

Join the DZone community and get the full member experience.

Join For Free

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 can’t 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 – let’s say you have a bucket definition in your Terraform code:

Properties files
 
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, we’ll 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 doesn’t accidentally become public).

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

Shell
 
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:

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


If you tried to run Terraform now, it would show you that there’s drift—and yes, there would be! We’ve just imported a bucket into a resource that still has old-name in the config. 


Step 3 - Change the Code

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

Properties files
 
resource “aws_s3_bucket” “my_bucket” {
    bucket = “new-name”
}


If you run Terraform now, you’ll 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: 

Shell
 
aws s3 rm s3://old-name --recursive
aws 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 You’re Using Spacelift

If you’re 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 you’re running the migration, this way nobody will accidentally run Terraform or make any other changes while you’re moving the data.

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

AWS Terraform (software)

Published at DZone with permission of Jacob Martin. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Implement Amazon S3 Cross-Region Replication With Terraform
  • Streamlining HashiCorp Cloud Platform (HCP) Deployments With Terraform
  • Automating AWS Infrastructure Testing With Terratest
  • Setting Up a ScyllaDB Cluster on AWS Using Terraform

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!