πŸ”§ Error Fixes
Β· 1 min read

Terraform State Lock Error β€” How to Fix It


Error: Error acquiring the state lock
Lock Info: ID: xxx-xxx-xxx

Another Terraform process (or a crashed one) is holding the state lock. Terraform locks state to prevent concurrent modifications.

Fix 1: Wait for the Other Process

# Someone else might be running terraform apply
# Check with your team first before force-unlocking

Fix 2: Force Unlock

# Get the lock ID from the error message
terraform force-unlock LOCK_ID

# Example
terraform force-unlock a1b2c3d4-e5f6-7890-abcd-ef1234567890

Only do this if you’re sure no other process is running.

Fix 3: Previous Run Crashed

# If terraform apply crashed (Ctrl+C, network drop, etc.)
# The lock is stale β€” safe to force unlock
terraform force-unlock LOCK_ID

Fix 4: DynamoDB Lock Table (AWS)

# Check the lock table directly
aws dynamodb scan --table-name terraform-locks

# Delete stale lock manually
aws dynamodb delete-item \
    --table-name terraform-locks \
    --key '{"LockID": {"S": "my-state-file.tfstate-md5"}}'

Fix 5: Disable Locking (Temporary)

# ⚠️ Only for emergency β€” risk of state corruption
terraform apply -lock=false

Fix 6: CI/CD Pipeline Timeout

# ❌ Pipeline killed terraform mid-run, leaving lock
# βœ… Add lock timeout
terraform apply -lock-timeout=5m

# Or in CI: always run force-unlock on failure
# GitHub Actions example:
# - run: terraform force-unlock $LOCK_ID || true
#   if: failure()