Terraform State Locked
Another Terraform operation is already running and has locked the state file.
Explanation
Terraform State Locked occurs when you try to run a Terraform operation (plan, apply, destroy) while another operation is already in progress. Terraform uses a state lock to prevent concurrent modifications that could corrupt the state file. This typically happens with remote state backends (S3, Consul, etc.) when two people or CI/CD pipelines run Terraform simultaneously, or when a previous Terraform run was interrupted (Ctrl+C, network failure) without releasing the lock.
Common Causes
- Another Terraform run in progress
- Previous run was interrupted
- CI/CD pipeline running concurrent applies
- Network failure during apply
- Manual state modification
Solution
First verify no other operation is actually running. If safe to proceed, force-unlock the state: terraform force-unlock LOCK_ID. Get the lock ID from the error message. Use terraform force-unlock -help to see options. For team environments, use Terraform Cloud or Atlantis for proper locking and workflow. Set up proper CI/CD pipeline to serialize Terraform runs. Use workspaces for parallel development. Check the state backend for stale locks. Always use terraform plan before apply to review changes.
Code Example
# Error: Error acquiring the state lock
# Error message: ConditionalCheckFailedException
# Get the lock ID from the error message
# Lock Info:
# ID: a1b2c3d4-e5f6-7890-abcd-ef1234567890
# Path: my-bucket/terraform.tfstate
# Operation: OperationTypeApply
# Check if someone else is running Terraform
# Ask your team before force-unlocking!
# Force unlock (use with caution)
terraform force-unlock a1b2c3d4-e5f6-7890-abcd-ef1234567890
# For S3 backend, check DynamoDB table
aws dynamodb scan --table-name terraform-locks
# Delete stale lock manually (DynamoDB)
aws dynamodb delete-item \
--table-name terraform-locks \
--key '{"LockID": {"S": "my-bucket/terraform.tfstate"}}'
# Preventive: use Terraform Cloud/Enterprise
# for proper state locking and team workflow
Error Information
Language
bash
Framework
terraform
Difficulty
Intermediate
Views
224
Related Errors
502
DevOps
Nginx 502 Bad Gateway
Nginx received an invalid response from the upstream server (PHP-FPM, Node.js, etc.).
503
DevOps
Service Unavailable (503)
The server is temporarily unable to handle requests due to overload or maintenance.
DevOps
Kubernetes OOMKilled
A container was killed by Kubernetes because it exceeded its memory limit.
CrashLoopBackOff
DevOps
Kubernetes CrashLoopBackOff
A container is repeatedly crashing and restarting in Kubernetes.