Terraform state confuses people at first. It's simpler than it sounds.
State is Terraform's memory. It's a file mapping each resource in your code to the real thing in the cloud. Without it, Terraform can't tell "this already exists" from "create a new one".
Rules I follow:
- Keep it remote. A state file on one laptop is a single point of failure and can't be shared.
- Lock it. Two people applying at once can corrupt state. A lock means the second run waits.
- Version it. If something goes wrong, you can roll the state file back.
- Never edit it by hand. Use
terraform statecommands, orimportandmovedblocks in code. - Treat it as sensitive. State can contain secrets, so lock down who can read it.
Get state right on day one and most Terraform horror stories never happen.