Skip to content

/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.tfstate in 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.
hcl
terraform {
  backend "s3" {
    bucket       = "example-tf-state"
    key          = "prod/network.tfstate"
    region       = "eu-west-1"
    use_lockfile = true
  }
}

Rules of thumb

RuleWhy
Never edit the state file by handuse moved, removed and import blocks, or terraform state commands
Never commit terraform.tfstate to Gitit can contain passwords and keys in clear text
Encrypt and restrict the backendstate is as sensitive as the credentials it contains
Keep one state per environment or componentsmaller blast radius, faster plans
Enable versioning on the bucketrecover 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