Terraform backend
The setting that decides where Terraform stores state and how it locks it, from a local file to shared cloud storage.
In more detail
The default local backend keeps terraform.tfstate on your disk, which is fine alone but gives a team no sharing, locking or recovery. Remote backends such as s3 store state in shared, access-controlled, versioned storage. Changing the backend requires terraform init again, with -migrate-state or -reconfigure.
Example
terraform {
backend "local" {
path = "terraform.tfstate"
}
}Related terms
- Terraform state — Terraform's record, usually a JSON file called terraform.tfstate, mapping each resource in the configuration to the real object it created.
- state locking — Preventing two Terraform runs from writing the same state at once, by taking a lock before any operation that could change it.
- S3 Versioning — An S3 bucket setting that keeps every version of every object, so overwritten or deleted objects can be recovered.
- use_lockfile — The s3 backend setting, added in Terraform 1.10, that locks state by writing a small lock file next to it in the bucket, with no extra database.
- object storage — Cloud storage that keeps whole files, called objects, in buckets and serves them over the network, such as Amazon S3.
- terraform init -migrate-state — The init option that copies existing state from the old backend into a newly configured one.
Related guides
Taught in
- Continuous IaC Drift Detection & State Protection — Cloud Security & DevSecOps Engineering
- Initialising The Working Directory — Infrastructure as Code with Terraform
- The Problem With Local State — Infrastructure as Code with Terraform
- Configuring A Remote Backend — Infrastructure as Code with Terraform
- State Locking With Use Lockfile — Infrastructure as Code with Terraform
- Workspaces For Multiple Environments — Infrastructure as Code with Terraform
- Security: Secrets In State And Ephemeral Values — Infrastructure as Code with Terraform
- One Hour, No Weights Published — HashiCorp Terraform Associate (004) Exam Prep
- State: Terraform's Memory Of What It Built — HashiCorp Terraform Associate (004) Exam Prep
- Write, Plan, Apply, And What Init Prepares — HashiCorp Terraform Associate (004) Exam Prep
- Validate, Format, Destroy, And The Flags That Bend A Plan — HashiCorp Terraform Associate (004) Exam Prep
- Resources, Data Sources And The References Between Them — HashiCorp Terraform Associate (004) Exam Prep
- Sensitive Data, Ephemeral Values And Secrets From Vault — HashiCorp Terraform Associate (004) Exam Prep
- Module Versions: Pin, Upgrade, Re-Initialise — HashiCorp Terraform Associate (004) Exam Prep
- The Local Backend, And Workspaces On One Machine — HashiCorp Terraform Associate (004) Exam Prep