Terraform State: What It Is and Why It Matters
State is how Terraform knows what it has already built. Understanding it prevents the most common and most painful mistakes.
Terraform compares three things on every plan: your configuration, the real infrastructure, and its state file. The state file records which real resources belong to which blocks in your code.
Why a local state file is risky
By default, state is written to a file on your machine. That works for experiments, but in a team two people can apply changes from different copies, and the file can contain sensitive values in plain text.
Use a remote backend with locking
A remote backend stores state in shared storage and locks it while a change is applied, so only one apply runs at a time. Access to that storage should be restricted, and encryption at rest enabled.
- Never edit state by hand. Use terraform state commands when you must change it.
- Treat state as sensitive data.
- Keep one state per environment so a mistake in staging cannot touch production.
- Terraform
- Infrastructure as Code