ShawnC
ShawnC

Reputation: 709

How to move Terraform state from one remote store to another

We use an Azure blob storage as our Terraform remote state, and I'm trying to move state info about specific existing resources to a different container in that Storage Account. The new container (terraforminfra-v2) already exists, and the existing Terraform code points to the old container (terraforminfra). I've tried the following steps:

  1. Use "terraform state pull > migrate.tfstate" to create a local copy of the state data in terraforminfra. When I look at this file, it seems to have all the proper state info.
  2. Update the Terraform code to now refer to container terraforminfra-v2.
  3. Use "terraform init" which recognizes that the backend config has changed and asks to migrate all the workspaces. I enter 'no' because I only want specific resources to change, not everything from all workspaces.
  4. Use the command "terraform state push migrate.tfstate".

The last command seems to run for a bit like it's doing something, but when it completes (with no hint of an error), there still is no state info in the new container.

Is it because I answer 'no' in step #3, does this mean it doesn't actually change to which remote state it "points"? Related to that, is there any way with the "terraform state" command to tell where your state is?

Am I missing a step here? Thanks in advance.

Upvotes: 6

Views: 8617

Answers (1)

ShawnC
ShawnC

Reputation: 709

OK, I think I figured out how to do this (or at least, these steps seemed to work):

  • rename the current folder with the .tf files to something else (like folder.old)
  • use "terraform state pull" to get a local copy of the state for the current workspace (you need to repeat these steps for each workspace you want to migrate)
  • create a new folder with the original name and copy your code to it.
  • create a new workspace with the same name as the original.
  • modify the code for the remote backend to point to the new container (or whatever else you're changing about the name/location of the remote state).
  • run "terraform init" so it's pointing to the new remote backend.
  • use "terraform state push local state file" to push the exported state to the new backend.

I then used "terraform state list" and "terraform plan" in the new folder to sanity check that everything seemed to be there.

Upvotes: 4

Related Questions