Score:0

Restoring Terraform state from backend

cn flag

My Terraform Git repository ended-up having some unreconciliable data error and I couldn't push a recent change. I exported my commit to a patch, recloned, applied the patch, and then realized I just blew-away all of my local state. I used AWS CLI to sync-down the whole tree of state files, copied the one into/as the .terraform/terraform.tfstate of the path that I wanted to apply, ran init, and am now getting a version error.

This is the exact same running environment that produced the state files in the first place. This is also the only machine that apply changes so everything should still be in sync (not that it relates to the immediate problem, but it won't cause any additional issues once we get past this).

Commands that I've tried running:

$ terraform init
$ terraform init -upgrade
$ terraform init -upgrade -migrate-state

The inevitable, unwavering output:

Initializing the backend...
╷
│ Error: Failed to load state: Terraform 1.3.9 does not support state version 4, please update.

I can't seem to find any posts or write-ups anywhere about how to restore from remote state, and yet I'm guessing that almost everyone has had to restore from remote state at some point. I can't understand it.

What should be the process to restore from remote? Why would I have version issues if the environment, project config, and project state haven't changed?

Thank you. Any help is appreciated.

Score:0
cn flag

When I ran terraform init, it had initialized the state to .terraform/terraform.tfstate . So, I had cleared the metadata and restored my state file to that file-path instead (a copy of the state that I had manually downloaded from S3). That's when I started seeing the error above. Not sure why. None of that makes sense. However, after noting a subtle reference from a post, I moved the state file back to the root of that project path and things magically worked. It looks like, when initializing a repository and dropping-in a state-file, it must be placed in the leaf terraform module as "terraform.tfstate", and terraform itself will then import it and place the data properly. After that, the original can probably be removed.

Note that the question itself arised from an imperfect understand of what the backend is. It's actually where the state will live, if not local. The local state file appears to just contains some reference/identity information used to access/process it. So, once I got my original state-file imported, it no longer changed with subsequent updates (as those changes are performed against that backend directly). This can be confirmed just by listing the state elements and then trying to print one with your network disconnected.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.