What is the Terraform state locking mechanism?
Answer
State locking prevents multiple Terraform runs from modifying state simultaneously, which could corrupt the state file. When Terraform starts a write operation (plan, apply, destroy), it acquires a lock. If another process already holds the lock, the second process waits. Backend-specific implementations: S3 + DynamoDB: Terraform writes a lock item to a DynamoDB table with the lock ID, caller, and timestamp. The item is deleted when the lock is released. Azure Blob Storage: uses Azure Blob lease (built-in). GCS: uses GCS object locking. Terraform Cloud: has built-in locking. If a process crashes while holding the lock, manually release it: terraform force-unlock <LOCK_ID>. Always configure state locking for any team environment. Without locking, two simultaneous terraform apply operations will race and can produce corrupted state.