Error Codes Wiki

Terraform State Lock Error — State File Locked by Another Process

Errorcommand

Overview

Fix Terraform state lock errors when another process holds the lock on the state file, preventing plan, apply, or destroy operations.

Key Details

  • Terraform locks the state file during write operations to prevent concurrent modifications
  • State locking prevents two terraform apply commands from running simultaneously on the same state
  • Remote backends (S3+DynamoDB, Terraform Cloud, GCS) provide distributed locking
  • Lock information includes: who locked it, when, and which operation
  • A stuck lock usually means a previous Terraform process crashed without releasing the lock

Common Causes

  • Previous terraform apply or plan was interrupted (Ctrl+C, crash, network disconnect)
  • Another team member is running Terraform against the same state file simultaneously
  • CI/CD pipeline hung or timed out while holding the state lock
  • DynamoDB lock table (for S3 backend) has a stale lock entry

Steps

  1. 1Check if another Terraform process is actually running — do not force unlock if it is
  2. 2Force unlock the state: 'terraform force-unlock LOCK_ID' (use the lock ID from the error message)
  3. 3For S3+DynamoDB backend: check the DynamoDB lock table for stale entries and delete manually if needed
  4. 4For Terraform Cloud: check the workspace runs page for stuck runs and cancel them
  5. 5Prevent future issues: use CI/CD pipelines with proper timeouts and ensure only one pipeline runs at a time

Tags

terraformstate-lockinfrastructureiacdevops

Related Items

More in Command

Frequently Asked Questions

Only if you are certain no other Terraform process is running against this state. Force-unlocking while another apply is in progress can corrupt the state file. Always verify first.