Runbook: restore a state file from an earlier version#
The state bucket keeps every superseded version of every state file for 90 days (noncurrent_version_retention_days in the seed). That is the recovery path for a state file that was overwritten, corrupted or deleted. This procedure has not yet been rehearsed (limitations).
Access: administrator access to the AWS account: the S3 console as the root user with MFA or OrganizationAccountAccessRole, or the AWS CLI with those credentials. No workflow restores state, and a restore needs judgement about which version is good, so it is done by hand.
When to do this#
- A plan wants to create resources that exist, or destroy ones that should stay: the state no longer describes the account.
- OpenTofu reports state it cannot read, or a lineage mismatch.
- A seed run moved a partial local state over the real one.
- A state file is missing.
If a plan merely shows changes, that is drift, not damage: see the drift runbook.
The state files#
All in the bucket testz-tfstate-<account-id>:
| Layer | Key | Written by |
|---|---|---|
| Seed | seed/terraform.tfstate | seed-account, or the seed from a laptop |
| Bootstrap | bootstrap/terraform.tfstate | bootstrap-account |
| Production | testz/prod/terraform.tfstate | terraform-apply |
Each has a lock object beside it (<key>.tflock) while a run holds it.
1. Stop writes#
Run no terraform-apply, bootstrap-account or seed-account until this is done. If a run is in progress, let it finish or cancel it, then check that no .tflock object remains for the key. Remove a lock only if no run holds it.
2. Find the last good version#
Console: S3 → the bucket → turn on Show versions → the key. Each version has a timestamp. Or:
aws s3api list-object-versions --bucket "testz-tfstate-<account-id>" \
--prefix testz/prod/terraform.tfstate \
--query 'Versions[].[VersionId,LastModified,Size,IsLatest]' --output table
The last good version is usually the one written by the last successful run before the damage. Match the timestamp against that run in Actions.
3. Check it before using it#
Download it and the current version, and compare:
aws s3api get-object --bucket "testz-tfstate-<account-id>" \
--key testz/prod/terraform.tfstate --version-id <version-id> candidate.tfstate
python3 -c 'import json,sys; s=json.load(open(sys.argv[1])); print(s["lineage"], s["serial"], len(s["resources"]))' candidate.tfstate
lineagemust match the current state's, unless the current one is the damage.serialis lower than the current state's.- The resource count is what the layer holds: compare with a version from before the damage.
Delete the downloaded files when done: state holds the account ID and other values kept out of the repository.
4. Restore it#
Copy the good version over the current object. The copy becomes the newest version; nothing is deleted, so this can itself be undone the same way.
In the console, download the version and upload it to the same key. Or, in one step:
aws s3api copy-object --bucket "testz-tfstate-<account-id>" \
--key testz/prod/terraform.tfstate \
--copy-source "testz-tfstate-<account-id>/testz/prod/terraform.tfstate?versionId=<version-id>"
The bucket's default encryption applies to the copy. Do not pass an encryption header other than aws:kms: the bucket policy refuses it.
5. Confirm#
- For production:
terraform-apply, plan only. The plan must show no changes, or only changes made since that version was written. Then rundrift-detection. - For the bootstrap layer:
bootstrap-account, plan only. The same test. - For the seed: a seed plan (
SEED_PLAN_ONLY=1, seeterraform/seed/README.md).
If the plan still wants to create existing resources, the version chosen was too old or too new: go back to step 2.
Afterwards#
Open an issue saying what damaged the state, and fix the cause through a pull request.