This content originally appeared on DEV Community and was authored by Vishwajeet Pratap Singh
State File Locking
Definition:
State file locking is a mechanism to prevent concurrent operations on the state file, ensuring consistency and avoiding race conditions.
Key Points:
Concurrency Control: Prevents multiple users from making changes to the infrastructure simultaneously, which could corrupt the state file.
Locking Mechanism: Implemented using various methods depending on the remote backend (e.g., DynamoDB for S3, Cosmos DB for Azure Blob Storage).
Automatic Handling: Terraform automatically locks the state file when running operations that modify the state and unlocks it when the operation completes.
Manual Intervention: In case of issues, locks can be manually removed or inspected.
Configuration Example (DynamoDB for S3):
resource “aws_dynamodb_table” “terraform_locks” {
name = “terraform-state-lock”
billing_mode = “PAY_PER_REQUEST”
hash_key = “LockID”
attribute {
name = “LockID”
type = “S”
}
}
This content originally appeared on DEV Community and was authored by Vishwajeet Pratap Singh