Terraform: surviving a corrupted state, repair guide without destroying everything
Un apply interrompu par un timeout réseau, un collègue qui lance un terraform apply depuis son poste pendant que la CI tourne, un state rm mal ciblé un vendredi soir : le résultat est toujours le même. Le state ne reflète plus l'infrastructure réelle, le plan propose de détruire une base de données de production, et toute l'équipe est bloquée. La tentation est alors forte de « repartir propre » :…
The article discusses common issues with Terraform state files and how to recover from them. When a Terraform state file becomes corrupted or diverges from the actual infrastructure, it can cause problems for the entire team. The state file is a database that maps resource addresses in the code to actual provider IDs and attributes.
Corruption can occur due to partial applies, manual changes in the console, refactoring of modules without proper updates, or concurrent writes. To address these issues, the article recommends freezing and saving the current state, then diagnosing the problem without making any changes.
It also provides steps for unlocking the state file properly, including inspecting and deleting locks from DynamoDB or S3. Additionally, it explains how to restore a previous version of the state file from S3 versioning, which can be a quick solution if the state file is truncated, empty, or invalid.
The article assumes familiarity with Terraform and AWS services like S3 and DynamoDB.
Summarised by urgent.news, not translated. Machine-written; may contain errors — check the original.
Source: dev.to