Home > OS >  How can I move Terraform state in an old S3 bucket to a new S3 bucket?
How can I move Terraform state in an old S3 bucket to a new S3 bucket?

Time:10-28

We have a few terraform configurations for which we use s3 as the backend. We have multiple AWS accounts, one for each of our environments.

In all the environments and across multiple region, we have different s3 bucket & dynamodb_table names used which as of now do not follow a valid convention and make it difficult to identify the purpose of buckets from its name. We now want to follow a convention based naming for all the s3 terraform state buckets. For this I will need to migrate the state of existing terraform resources to the new s3 buckets.

I am not sure what would be the best way to achieve this without having to run destroy on old state bucket and apply on the new one.

A few options that I could think of were as below but I am not sure if they are the right thing to do.

  1. Rename the s3 bucket: I understand this is not possible (or is it?)
  2. Move the s3 bucket: A solution to renaming is as mentioned here. Not sure if this approach will disturb the setup we have.
  3. State migration: The terraform source code is Git controlled but .terraform is not. So, I can make changes to the source code with new bucket, commit it and create a new git tag from this commit. Now when actually doing the migration, I would checkout old git tag being used in my environment, run terraform init, checkout the new git tag and run terraform init again which would ideally ask me if I want to do a migration and do the needful. This process is something similar to what terraform suggests here, but what I am not sure is, will this approach work in the kind of movement that I am expecting to do.

P.S.: I assume renaming the DynamoDB table or just using a new one instead of old would work out of the box as I would be making sure that when doing the state migration, I do not have any live terraform runs in progress.

CodePudding user response:

Move your Terraform state files to the new S3 buckets and then change the bucket parameter inside your S3 backend config to point to the bucket.

You do not need to do anything else if you just want to move the location of the state file - it's like changing the location of an application and then pointing the shortcut to the new location.

CodePudding user response:

Did you try to copy the StateFile From old bucket to new bucket and then change the S3 bucket in terraform backend configuration

  • Related