Skip to content

tfstate

  • terraform.tfstate is a json 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 first terraform apply command. And then modified on subsequent apply commands

Best practices

  1. Never manually modify the tfstate. Always change it through tf commands (terraform apply, terraforma state)
  2. 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
  3. Use state locking
    • Prevent concurrent runs to your state file
    • This should be configured at the storage backend
  4. Enable versioning
    • Allows reversing to any previous state
    • This should be supported by the storage backend
  5. Use one state file per environment
    • Dev, test, staging, prod