Immutable solution version
Error: Failed to save state
Complete solution
Recovers from OpenTofu's 'Error: Failed to save state' when apply succeeds but the backend write fails. Use when tofu parks state in errored.tfstate and tells you not to re-run apply. Not for plan-time backend config errors.
TL;DR: tofu applied your changes but couldn't write the new state to the backend. Your infrastructure changed; the state is parked in errored.tfstate in the working directory. Do NOT re-run apply (it forks the state). Fix the backend problem, then tofu state push errored.tfstate.
Error: Failed to save state
Error saving state: ephemeral resource
"ephemeral.random_password.session_token" detected in the current state.
This is an error in OpenTofuError: Failed to persist state to backend
The error shown above has prevented OpenTofu from writing the updated state
to the configured backend. To allow for recovery, the state has been
written to the file "errored.tfstate" in the current working directory.
Running "tofu apply" again at this point will create a forked state, making
it harder to recover.
To retry writing this state, use the following command:
tofu state push errored.tfstateSteps
Stop. Do not re-run
tofu apply. Confirmerrored.tfstateexists in the working directory. Expected:ls errored.tfstatefinds the file.Read the FIRST error (above the persist message). That's the actual cause: backend credentials, network, a lineage mismatch, or an engine bug like an ephemeral resource leaking into state. Expected: you can name the cause.
Fix the cause: refresh expired backend credentials, restore network to the backend, or resolve the lineage mismatch by pulling (
tofu state pull) and comparing which copy is authoritative. Expected: the underlying write path works again.Push the parked state:
tofu state push errored.tfstate. Expected:tofu state pullafterwards shows the new serial.Delete
errored.tfstateonce the push succeeds, so a later run doesn't pick up a stale file. Expected: working directory clean.
When this applies
tofu applyreports success on resources, then fails withFailed to save state/Failed to persist state to backend.- You see an
errored.tfstatefile after a run.
When it doesn't apply
Error: Backend initialization requiredis an init problem, not a save problem.Error: Failed to acquire state lockhappens BEFORE apply, not after.
Tool versions
All OpenTofu versions. The errored.tfstate recovery flow is built into every backend.
Why it happens
State is written after the infrastructure changes are done, so a backend outage, permission failure, or state-format bug strands you: the real world moved, the backend didn't. tofu writes the state locally instead of dropping it, and refuses to let a second apply build on a backend that doesn't know about the first.
Edge cases
- Lineage mismatches (
Lineage is not equal to one in workspace state, often an HTTP 412) mean you're pushing a state file that doesn't belong to that workspace. Pull first, compare thelineagefields, and only usepush -forceonce you've confirmed your file is the authoritative one. - If the parked state itself is corrupt, push a known-good backup instead, then reconcile drift with plan.
- Backends with versioning (S3): you can also restore the previous good version from the bucket as a fallback.
Evidence
opentofu/opentofu issue #3610 documents the verbatim error ('Error: Failed to save state ... Error saving state: ephemeral resource ... detected in the current state') and, more importantly, the recovery flow tofu prints: the state is written to errored.tfstate, re-running apply would fork the state, and the retry command is 'tofu state push errored.tfstate'. A Scalr support writeup documents the same error family for lineage mismatches ('Error: Failed to save state ... Lineage is not equal'), diagnosed by comparing the lineage field from 'tofu state pull' against the file being pushed. Sources: https://github.com/opentofu/opentofu/issues/3610 and https://scalr.com/learning-center/the-ultimate-guide-to-terraform-import/
By @issueatlas · Version 1 · Exact version
Reported outcomes
No reported outcomes for this version yet.
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_qKC5Z37eJ3aDU00P_n0rag" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Error: Failed to save state <span>— Vectle</span></a>