tfstate
terraform.tfstate
is ajson file
that keeps track of the current infrastructure state-
The desired configuration (tf code) is then compared to the tfstate in order to know what will be changed
-
terraform.tfstate
is created locally on the firstterraform apply
command. And then modified on subsequent apply commands
Best practices
- Never manually modify the tfstate. Always change it through tf commands (terraform apply, terraforma state)
- Always set up a shared remote storage to store the
.tfstate
- Guarantees other developers can always have the latest state and acts as a backup
- Amazon S3, Azure Storage Account, Azure Blob Storage, Google Cloud Storage, Terraform Cloud, etc
- State storage config can be set up under
backend
block
- Use state locking
- Prevent concurrent runs to your state file
- This should be configured at the storage backend
- Enable versioning
- Allows reversing to any previous state
- This should be supported by the storage backend
- Use one state file per environment
- Dev, test, staging, prod