Published : 2021-03-30

Terraform - recover from lost state

Terraform is a very nice IAC tool, and its major force, but also weakness is its stateful model.

Stateful model permits to have proper idempotency on changes. State is a file which is stored somewhere and loosing it will immediately trigger idempotency issues. Whereas local file is clearly not recommended as state can contains secrets, and git is not a backup solution, using a S3 like or consul backend can help to ensure state is consistently replicated on the backend, and optionnaly (recommended) versionned.

Despite all those insurances, you may loose for any reason your state, ie. a manual human error.

If your providers are correctly developped you can import existing resources in the newly created state in order to recover.

Imagine your state has consul ACLs for example, here is a way to reimport the resource:

terraform import -var-file=../../global.tfvars consul_acl_policy.prometheus b901a93a-cccc-bbbb-aaaa-8e10dad1efff

This will connect to your consul and retrieve the b901a93a-cccc-bbbb-aaaa-8e10dad1efff and bind it with prometheus consul acl policy object in your terraform file.