State Versioning and History
What you'll learn: How to preserve a complete timeline of your infrastructure state changes so you can recover from mistakes or understand what changed over time.
The Time Machine for Your Infrastructure
When you enable versioning on your Terraform state backend storage (like S3, Azure Storage, or GCS), you create a safety net. Every time Terraform writes a new version of your state file, the backend keeps the old versions instead of overwriting them. Think of it like Git for your infrastructure's actual runtime state—each change creates a new snapshot you can revisit.
Without versioning, a corrupted state write, accidental deletion, or bad apply could leave you with no way back. With versioning enabled, you have a complete audit trail and can restore any previous state snapshot.
Enabling Versioning on Your Backend
For S3 backends, enable versioning on your bucket through the AWS Console or CLI—this is a bucket-level setting, not a Terraform configuration.
For Azure Storage, enable blob versioning on your storage account.
For GCS, enable object versioning on your bucket.
Once enabled, every state write creates a new version automatically. The backend stores metadata like timestamps and version IDs, letting you list history and retrieve older versions if needed.
Why This Matters
State versioning supports critical rollback scenarios: if a bad deployment corrupts your state or you need to understand exactly when a resource configuration changed, you can retrieve and restore a previous version. This is essential for disaster recovery, compliance auditing, and troubleshooting drift that appeared after specific changes.
Combined with proper access control and encryption (from your previous lessons), versioning completes your state file protection strategy.
Key Takeaway: Enable versioning on your state backend storage to maintain a complete history of state changes, enabling rollback, audit trails, and recovery from state corruption or accidental deletions.