Terraform Backend
A Terraform backend defines where Terraform stores its state and how operations run. The default is a local file, while teams use remote backends like S3, GCS, Azure Blob, or Terraform Cloud.
What Is a Backend?
A remote backend gives every engineer and pipeline one shared state, with locking and history.
| Backend | State storage | Locking |
|---|---|---|
| local | Local disk | Single machine |
| s3 | AWS S3 | Lock file or DynamoDB |
| gcs, azurerm | Cloud storage | Built in |
terraform init # configure a new backendterraform init -reconfigure # use new settings, no migrationterraform init -migrate-state # copy state to the new backendBackend blocks cannot use variables. For per-environment values, leave the block empty and pass -backend-config flags at init.
Common MistakeManaging the state bucket in the same configuration that stores state there. Bootstrap it separately.
Frequently Asked Questions
What is the difference between a backend and a provider?
A provider talks to a platform like AWS to manage resources. A backend decides where state is stored.
What should I do before migrating from local to remote state?
Back up terraform.tfstate, have one person migrate, run terraform init -migrate-state, then confirm terraform plan shows no unexpected changes.