1
votes

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.

1
You could just copy the files across and update the configuration to use the new location I think.ydaetskcoR
@ydaetskcoR, that sounds way too simple. :-) I've seen so many blog posts on doing this with slight variations in the steps, I would have expected that if copying the files worked, that's what everyone would be saying ...?ShawnC
It works for me when I'm refactoring state files to be in different places. For example I might change the directory path for some Terraform code and my wrapper scripts automatically use the path from the root of the repo to to set the state file location. If I want to change that I just copy the state files to the new location and then move the files in my repo and Terraform will show an empty diff. Once it's all merged and everything is using the new location I manually delete the old, unused state files.ydaetskcoR
If you were using some state backend where i'ts not simple files or the backend configuration is more complex (eg Consul or a database and/or when migrating between backend types) then pulling the state using the Terraform CLI locally so you have a local file of state and then pushing it to the new backend would work but it's overly complex if you don't need that flexibility.ydaetskcoR
That's effectively what we have ... the state data in Azure blob storage is not in files, per se (they're just "blobs" of JSON), so nothing you can just copy like they were.ShawnC

1 Answers

0
votes

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.