Why use remote state with locking, and how?
⚡
Quick Answer
Remote state (e.g. S3 + DynamoDB) shares state across a team and locks it to prevent concurrent applies from corrupting it.
Detailed Answer
Local state doesn't work for teams — two applies race. A remote backend stores state centrally and, with a lock (DynamoDB for S3, or native locking in Terraform Cloud/GCS), serializes operations so only one apply mutates state at a time. It also enables encryption and versioning.
Code Example
terraform {
backend "s3" {
bucket = "tf-state"
key = "prod/terraform.tfstate"
dynamodb_table = "tf-locks"
}
}💡
Interview Tip
Name the S3 + DynamoDB lock pattern.
terraformremote-statelocking