Skip to main content
Fix Terraform Error - State Lock Acquisition Failed

Fix Terraform Error - State Lock Acquisition Failed

Key Takeaway

How to fix 'Error acquiring the state lock' in Terraform. Safely unlock state files locked by crashed or interrupted operations.

Table of Contents

The Error

Error acquiring the state lock: ConditionalCheckFailedException

What Causes This

Terraform locks state files to prevent concurrent modifications. If a terraform apply crashes, gets killed, or times out, the lock may remain. This prevents any further operations until the lock is released.

How to Fix It

Solution 1: Force Unlock (Use With Caution!)

# Get the Lock ID from the error message
# Error message shows: Lock Info: ID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

terraform force-unlock LOCK_ID_HERE

⚠️ Only use force-unlock when you’re certain no other operation is running!

Solution 2: Check DynamoDB Lock Table

# If using S3 backend with DynamoDB locking
aws dynamodb scan \
  --table-name terraform-state-lock \
  --filter-expression "attribute_exists(LockID)"

# Delete stale lock manually
aws dynamodb delete-item \
  --table-name terraform-state-lock \
  --key '{"LockID": {"S": "your-bucket/path/terraform.tfstate"}}'

Solution 3: Verify No Running Operations

# Check if another terraform process is running
ps aux | grep terraform

# Check CI/CD pipelines for running jobs
# GitLab, GitHub Actions, Jenkins — make sure no parallel runs

Solution 4: Prevent Future Lock Issues

# Set a reasonable lock timeout
terraform {
  backend "s3" {
    bucket         = "my-terraform-state"
    key            = "prod/terraform.tfstate"
    region         = "us-east-1"
    dynamodb_table = "terraform-state-lock"
  }
}
# Use -lock-timeout to wait instead of failing immediately
terraform apply -lock-timeout=5m

Prevention Tips

  1. Pin provider versions — avoid surprise breaking changes
  2. Use CI/CD — catch errors before they hit production
  3. Test with terraform plan — always review before applying
  4. Keep Terraform updated — newer versions have better error messages
  5. Use terraform validate — catches syntax errors early

Hands-On Courses

Learn to avoid these errors with interactive, project-based courses:

Conclusion

Related: How to install Terraform on macOS — our most popular installation guide.

This error is common and fixable. Follow the solutions above, and check our Terraform course for hands-on training that covers real-world troubleshooting scenarios.

🚀

Level Up Your Terraform Skills

Hands-on courses, books, and resources from Luca Berton

Luca Berton
Written by

Luca Berton

DevOps Engineer, AWS Partner, Terraform expert, and author. Creator of Ansible Pilot, Terraform Pilot, and CopyPasteLearn.