State version created by 'state mv' command not supported?

I needed to move resources between two different backend states using terraform v0.14.2. I ran the command:

terraform state mv -state-out ../new-module-location/.terraform/terraform.tfstate <resource-instance-address> <resource-instance-address>

for each resource in the state file.
I then expected that running a terraform init in that folder would write the local state to the configured remote state location. However, instead I got the error message:

Error: Failed to load state: Terraform 0.14.2 does not support state version 4, please update.

What have I done wrong? How have I managed to create a local state file using terraform 0.14.2 that can’t be read with terraform 0.14.2?

Other things I tried previously:

  • I originally called the local state file instance tfstate. When I ran a terraform init it initialised successfully, but on running a terraform plan was unaware of the existing resources, thinking instead that it had to create them all. I expect this is because it wouldn’t have known about the tfstate file.
  • I tried moving the tfstate file to .terraform/terraform.tfstate and then running terraform init. This came up with the error message.

Hi @steven.hirschorn,

In spite of its filename, the file at .terraform/terraform.tfstate is not a state file in the sense that terraform state mv means. In much older versions of Terraform it was a state file, but in modern Terraform it only stores a pointer to your state saved in a remote system. That file is not valid to use with any command other than terraform init (which writes to it and reads from it it implicitly).

If you are using remote state then you should not use the -state or -state-out arguments to any Terraform commands. Those arguments are for local state only and are only still available for backward compatibility with Terraform versions that predate the concept of backends.

Given your goal of moving something from one state to another, I don’t think there’s a way to achieve that with only a single command. The terraform state mv command (and the other similar terraform state commands) are for manipulating the current state directly, not for taking actions between different state snapshots.

Instead, I would suggest trying to terraform import the object you want to move into the new state, and then if successful use terraform state rm to remove it from the old. This will work only if the resource type in question is importable and if you have the relevant access to retrieve the object from the remote system in order to import it.

1 Like

Thanks very much, @apparentlymart !

I’d been following notes I’d used successfully previously to move between remote states, and originally picked up from this article:

In any case, all the resources were importable, so I’ve moved them that way. Thanks for your speedy and detailed response!