Prompt
I need state locking for Terraform in a team environment
Latest observation
Oct 25, 2026
OpenAI APIWeb search: off
For a team environment, you should enable Terraform state locking so two people or automation jobs can’t modify the same state at the same time.
Why it matters
Without locking, concurrent terraform apply runs can:
- overwrite each other’s state changes
- create partial/invalid infrastructure
- cause drift and hard-to-debug failures
Recommended options
Terraform locking depends on the backend you use:
1) S3 backend + DynamoDB lock table (common AWS setup)
Use:
- S3 for remote state storage
- DynamoDB for state locking
Example:
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "envs/prod/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "terraform-locks"
encrypt = true
}
}
Create the DynamoDB table with:
- partition key:
LockID(string)
2) Terraform Cloud / Terraform Enterprise
This is often the easiest for teams:
- automatic state locking
- remote runs
- access controls
- run history and collaboration
3) Azure Blob Storage backend
AzureRM backend supports locking via blob leases.
4) GCS backend
Google Cloud Storage backend supports state locking via object generation / preconditions.
Best practices for team use
- Use a remote backend; don’t keep state locally
- Store one state per environment or per stack
- Restrict access with IAM/RBAC
- Enable encryption at rest
- Use CI/CD with controlled approvals
- Avoid manual simultaneous applies
- Consider separate workspaces only when appropriate; often separate state files are clearer
Important note
If you use a backend that doesn’t support locking, Terraform will warn you or not protect you. In a team setup, that’s risky.
If you want, I can give you:
- a complete AWS S3 + DynamoDB locking setup, or
- the best locking approach for your cloud/provider.