Skip to content
shiporbreak

No streak yet

Sign in

November 6, 2026 路 #51

TerraformMediumOpen write-up

Two applies, one state file

Two engineers ran terraform apply at the same time from their laptops. Now Terraform wants to recreate half the VPC. Which line is the root cause?

terraform {
  required_version = ">= 1.6"
  backend "local" {
    path = "terraform.tfstate"
  }
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

Next puzzle in UTC

One try. The first answer sticks.

How to fix it

Each laptop had its own state file and nothing locked it. Each apply only knew its own history. A remote backend with locking gives one state and one writer at a time.

Terraform state is the map from your code to real resource IDs. With a local backend, every engineer has a different map. When one person applies, the other person's state does not know those resources exist, so the next plan proposes creating duplicates or destroying things it believes are orphaned. Without a lock, two applies at the same moment can also interleave writes against the same resources.

Fix

  backend "s3" {
    bucket       = "acme-tfstate"
    key          = "network/terraform.tfstate"
    region       = "us-east-1"
    encrypt      = true
    use_lockfile = true
  }

use_lockfile uses S3 native locking on Terraform 1.10 and later. Older setups use a DynamoDB lock table. Turn on bucket versioning so a bad state write can be rolled back, and run applies from CI rather than laptops.

Sponsor this spot

Put your tool on the puzzle
One labeled card, in front of engineers who show up every day from YouTube, TikTok, and Instagram.