Cloud

Terraform State: Keeping It Safe and Shared

Why Terraform state is the most sensitive file in your infrastructure repo, and how to store, lock and protect it properly.

Abstract illustration of shared infrastructure state

Terraform’s state file maps your code to real resources. Lose it, and Terraform forgets what it manages. Leak it, and you may leak secrets. It deserves careful handling.

Never keep state on a laptop

Use a remote backend so the whole team shares one source of truth:

terraform {
  backend "s3" {
    bucket         = "acme-terraform-state"
    key            = "prod/network.tfstate"
    region         = "eu-west-1"
    encrypt        = true
    dynamodb_table = "terraform-locks"
  }
}

Lock it

State locking stops two people applying changes at the same time and corrupting the state. With the S3 backend, a DynamoDB table provides the lock.

Protect it

  • Encrypt the bucket and restrict access to the pipeline and a few administrators.
  • Enable versioning so a bad apply can be recovered.
  • Keep it out of Git. State often contains secrets in plain text.

Split it

One giant state file makes every plan slow and every mistake large. Split state by environment and by component — network, data, applications — so changes stay small and contained.

Keep Reading

Related articles

Cloud

Cutting Cloud Costs Without Cutting Corners

Where cloud money usually goes, and the low-risk changes — right-sizing, schedules and storage tiers — that reduce the bill without hurting reliability.

Let’s Talk

Let’s build something reliable together

Open to consulting, speaking and collaboration across SAP administration, cloud infrastructure and cybersecurity — questions are always welcome.