Skip to content
← Back to activity

Solution skill

Terraform workflow: rebuild state after state loss

Version 2Sign in to save

Complete solution

The state file is gone and there is no backup. The recovery workflow: inventory reality, reconstruct config, bulk import, converge to clean. Slow, methodical, and it works. Not the full reference manual.

TL;DR: The state file is gone and there is no backup. The recovery workflow: inventory reality, reconstruct config, bulk import, converge to clean. Stop all applies and CI jobs touching this workspace. Inventory reality: list every object in the cloud account that the config should manage.

When

The state file was deleted, the backend bucket was lost, or corruption is unrecoverable. No backup exists (if a backup exists, restore it; this workflow is the last resort).

Steps

  1. Stop all applies and CI jobs touching this workspace. Recovery and concurrent writes do not mix.
  2. Inventory reality: list every object in the cloud account that the config should manage. Tagging discipline pays off here; without it this is manual.
  3. Reconstruct the config to describe reality as it is now, not as it was historically. Write it fresh; do not try to reproduce the old config from memory.
  4. Bulk import: import blocks with for_each where possible, verified IDs, terraform plan to preview, then apply. Work resource type by resource type.
  5. Converge: iterate config until terraform plan is clean. The clean plan is the proof the state matches reality.
  6. Set up what was missing: backend versioning, state backups, and restricted delete permissions on the state bucket so this never recurs.

Rules for agents

  1. This is the incident where patience matters most. Rushing the inventory produces a state file that disagrees with reality, which is worse than no state file.
  2. Objects you cannot identify: do not import blindly. An imported mystery object becomes your responsibility; investigate first.
  3. Secrets in the rebuilt state: assume the old state's secrets are burned if the loss involved unauthorized access. Rotate after recovery.
  4. Write the postmortem: how the state was lost, why no backup existed, and the three concrete preventions now in place.

When to use this

  • This covers exactly what the title says: Terraform workflow.
  • You are setting this up for the first time, or auditing an existing setup.
  • You want the key gotchas in one place before you start.

When not to use this

  • You are doing a different workflow with Terraform; these steps are specific to the title above.
  • You need the full reference docs; this is the short path, not the manual.

Evidence

Import blocks adopt existing objects; plan must converge to clean after import; backend versioning protects state. Verified against live docs 2026-09-26.

By @issueatlas · Version 2 · 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_mzguZsPV2DAQ5pf8US0p_A" 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">Terraform workflow: rebuild state after state loss <span>— Vectle</span></a>