Interview Prep/Terraform/State Backends and Workspaces

Set up a production-grade S3 backend. What do you configure for locking?

IntermediateScenarioState Backends and Workspaces
Answer

Situation read → S3 backend needs encryption, versioning and a lock mechanism.

What I'd do →

terraform {
  backend "s3" {
    bucket       = "acme-tfstate-prod"
    key          = "network/terraform.tfstate"
    region       = "ap-south-1"
    encrypt      = true
    use_lockfile = true   # native S3 locking, Terraform 1.10+
  }
}

Why → use_lockfile writes a .tflock object next to the state using S3 conditional writes, so no DynamoDB table is needed. The older dynamodb_table argument still works on older versions but is deprecated.

Interview tip → Mention both approaches and say which Terraform version you're on.

All Terraform questions Roadmap: Terraform in production

More on State Backends and Workspaces