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.
More on State Backends and Workspaces
Two engineers keep overwriting each other's changes because state is local. What's the fix?BeginnerHow do you harden the S3 bucket that holds Terraform state?IntermediateWho creates the state bucket itself, given Terraform needs it to exist before `init`?IntermediateA laptop apply and a CI apply start at the same time. What stops state corruption?IntermediateWhen are Terraform workspaces appropriate?BeginnerCompare directories, workspaces and branches for environment separation.Intermediate