← All posts

Terraform Backend: Why Remote State Is a Must

terraformopentofuiacdevopsstatebackendaws

The first time you run terraform apply with Terraform, a terraform.tfstate file quietly shows up in your directory. When you're working alone on a personal project, keeping that file locally is fine. But the moment a second person joins the team or you move to CI/CD, this small file can turn into your biggest headache. In this post we'll look at what a Terraform backend is, why you shouldn't keep state locally, and which problems a remote backend solves. (If you're new to Terraform, you might want to read Infrastructure as Code: Terraform first.)

What Is a Backend?

A backend is what decides where Terraform stores its state file. There are two basic types:

  • Local backend (default): This is what you get if you don't configure anything. State is kept on your local disk as terraform.tfstate in the directory you run from.
  • Remote backend: State is kept in a shared, remote location. You're not locked into AWS here. AWS S3, Google Cloud Storage, Azure Blob, Cloudflare R2, Terraform/OpenTofu Cloud and more are supported. (Terraform itself works the same way with hundreds of providers. AWS, GCP, Azure, GitHub, Kubernetes, you can manage pretty much anything that has an API.)

State is Terraform's map between your code and the real infrastructure. It's how Terraform knows which resources it manages, along with their IDs and attributes. So if the state is lost, Terraform no longer "recognizes" the infrastructure it manages. That's why where and how it's stored matters so much.

Problems with Local State

The local backend is simple, but it starts to crack as soon as you go beyond a one-person setup.

Problems:

  • Teamwork is impossible: The state lives on your disk, so your teammates can't get to it. Putting it in Git leaks sensitive data and produces constant merge conflicts.
  • No state locking: If two people (or two pipelines) run apply at the same time, the state gets written from two places at once and ends up corrupted (race condition).
  • Single point of failure: If your disk dies or you delete the file by accident, Terraform completely loses track of the resources it manages.
  • Security hole: The state file can contain secrets (database passwords, API keys) in plain text, and it sits unprotected on your local disk or in Git.

Why Remote Backend Is a Must

A remote backend solves all of the problems above in one go.

Advantages:

  • Shared state: The whole team and every CI/CD pipeline see the same single source of truth.
  • State locking: While an apply is running, the state is locked, and a second apply waits until the lock is released. Corruption from concurrent writes becomes impossible.
  • Durability and versioning: With bucket versioning, past versions of the state are kept, so you can roll back when something goes wrong.
  • Security: Encryption-at-rest and access control (IAM/policy) protect state that contains secrets.
  • CI/CD friendly: Automation pipelines work against a central, reliable state.

Remote Backend Configuration (AWS S3)

Keeping state in AWS S3 is the most common approach. All you need is a backend definition in the terraform block:

backend.tfhcl
terraform {
  backend "s3" {
    bucket       = "acme-terraform-state"
    key          = "prod/terraform.tfstate"
    region       = "eu-central-1"
    encrypt      = true
    use_lockfile = true
  }
}

Code Explanation:

  • backend "s3": Says that state will be stored in AWS S3.
  • bucket / key: Tell Terraform which bucket and which path the state file lives in. Separating environments (prod/, staging/) under different keys is good practice.
  • region: The region the state bucket is in.
  • encrypt = true: State is stored with server-side encryption (SSE).
  • use_lockfile = true: Provides state locking with a lock file on S3. (Before this feature you needed a separate DynamoDB table for locking. Now S3 on its own is enough.)

After adding the backend block, run terraform init. Terraform will offer to move your existing local state to the remote backend:

bash
terraform init -migrate-state

Same Idea, Different Provider: Cloudflare R2

Cloudflare R2 is S3-compatible, so you can use the same s3 backend with a custom endpoint. That gets you the same setup without S3's egress fees:

backend.tfhcl
terraform {
  backend "s3" {
    bucket = "acme-terraform-state"
    key    = "prod/terraform.tfstate"
    region = "auto"

    endpoints = {
      s3 = "https://<account_id>.r2.cloudflarestorage.com"
    }

    use_lockfile                = true
    skip_credentials_validation = true
    skip_region_validation      = true
    skip_requesting_account_id  = true
    skip_s3_checksum            = true
  }
}

The skip_* flags turn off AWS-specific checks, which makes it possible to work with S3-compatible providers. The concept stays the same. Only the address of the state changes.


How Does State Locking Work? (Mermaid Diagram)

The diagram below shows the lock kicking in when two developers run apply at the same time:

graph TD
    A["Developer A"] -->|terraform apply| L{State Lock}
    B["Developer B"] -->|terraform apply| L
    L -->|"A takes the lock"| C["Remote state is updated"]
    L -.->|"B is locked, waits"| E["Apply is blocked"]
    C --> S[("Remote Backend - S3 / R2")]
    S -->|"lock is released"| B

As long as Developer A holds the lock, Developer B's apply is blocked. When A finishes and releases the lock, B safely continues from where it left off. Thanks to this mechanism, the state is never written from two places at once.


Best Practices

  • Never put state in Git. Add *.tfstate and .terraform/ to your .gitignore.
  • Turn on versioning + encryption. Enable version history and encryption on the state bucket.
  • Separate your environments. Use a separate state key or workspace for each environment (prod, staging).
  • Principle of least privilege. Only give access to state to the people and services that need it, with minimum permissions.

Conclusion

Local state is perfectly fine while you're learning Terraform and for solo experiments. But the moment your infrastructure gets serious, the team grows or you move to automation, a remote backend stops being an option and becomes a requirement. Shared state, locking, durability and security all come with a single backend block. This small investment saves you from "the state is corrupted" disasters down the road.

Resources: