/glossary/terraform/terraform-state
What is Terraform state?
The commands on this page have not been executed by our verification runner yet.updated 2026-10-20
- tool
- Terraform
- versions
- Terraform 1.x, OpenTofu 1.x
- os
- any
Terraform state is the record Terraform keeps of what it manages. For every resource address in the configuration (aws_s3_bucket.logs, module.net.aws_vpc.main) it stores the ID of the real object and its attributes after the last apply. On each plan Terraform compares three things: the configuration, the state, and what the provider reads back from the real infrastructure.
Where it lives
- Local:
terraform.tfstatein the working directory, the default. Fine for experiments only. - Remote backend: an S3 bucket, Azure Storage container, Google Cloud Storage bucket, HCP Terraform and others. Remote state can be shared by a team and locked during operations so two applies cannot run at once.
terraform {
backend "s3" {
bucket = "example-tf-state"
key = "prod/network.tfstate"
region = "eu-west-1"
use_lockfile = true
}
}Rules of thumb
| Rule | Why |
|---|---|
| Never edit the state file by hand | use moved, removed and import blocks, or terraform state commands |
| Never commit terraform.tfstate to Git | it can contain passwords and keys in clear text |
| Encrypt and restrict the backend | state is as sensitive as the credentials it contains |
| Keep one state per environment or component | smaller blast radius, faster plans |
| Enable versioning on the bucket | recover from a bad apply or a corrupted state |
Drift is the difference between state and reality, for example a setting changed in the cloud console; terraform plan -refresh-only shows it.
Sources
Related pages
cheat sheets · terraformTerraform cheat sheet: init, plan, apply, state and workspacesTerraform cheat sheet: init and upgrade, fmt and validate, plan and apply, outputs, state list, mv and rm, import, workspaces, and common language syntax.flags · terraformterraform plan -out: save a plan and apply exactly that planterraform plan -out=tfplan saves the plan to a file; terraform apply tfplan then applies exactly those changes without a prompt. Inspect it with terraform show.compare · terraformTerraform count vs for_each: which one to usecount creates numbered copies addressed by index, so removing an item shifts the rest; for_each keys instances by name and keeps them stable. Which to use.