The terraform.tfstate is the map between your code and the real world. Lose the state, lose control of your infrastructure.
What the state stores
Every resource created, their real IDs in the cloud, dependencies, and metadata. This is how Terraform knows what to create, change, or destroy in a plan.
Why local state is dangerous
- No teamwork: everyone has a different state
- No locking: two simultaneous
applyoperations corrupt everything - Secrets can be stored in plain text in the file
Remote Backend with S3
terraform {
backend "s3" {
bucket = "meu-terraform-state"
key = "prod/network.tfstate"
region = "us-east-1"
encrypt = true
use_lockfile = true
}
}
After configuring:
terraform init -migrate-state
terraform plan
Golden Rules
- State always remote, versioned, and encrypted
- Never edit the state manually — use
terraform state mvand similar commands - One state per environment (dev/staging/prod separated)