Terraform & IaC (50 Qs)•Part 1: Terraform Core Architecture, HCL & Providers (1 - 15)
How do you configure a Production Remote Backend with AWS S3 and DynamoDB State Locking?
senior LevelCode ExampleFormsCI/CD & DevOpsSystem Design
In Simple Words:
Storing state locally on a developer's laptop causes state desynchronization, lost state on laptop failure, and race conditions if two engineers run apply simultaneously.
Production S3 + DynamoDB Backend:
- S3 Bucket: Stores the state file with Versioning and Server-Side Encryption (KMS) enabled.
- DynamoDB Table: Provides distributed state locking via a
LockIDprimary key. When someone runsterraform planorapply, Terraform acquires a lock. Anyone else attempting to run receives an error until the operation completes.
hcl
terraform {
backend "s3" {
bucket = "mycompany-terraform-state-prod"
key = "networking/vpc.tfstate"
region = "us-east-1"
dynamodb_table = "terraform-state-locks"
encrypt = true
}
}