Terraform State Lock: How It Works and How to Fix Stuck Locks
This article covers how state locking works across different cloud providers, how modern S3 native locking operates without DynamoDB, and how to safely clear a stuck lock using the force-unlock command.
What is terraform state locking and why is it important
Terraform state locking is a concurrency control mechanism that prevents multiple operations from simultaneously modifying the same Terraform state file. It protects your infrastructure from race conditions and data corruption. When you run an apply or plan command, Terraform automatically attempts to acquire a lock on your remote backend.

State locking implements pessimistic locking at the granularity of the entire state file. Active modification locks the whole file. As a result, this protects your underlying infrastructure from race conditions where two simultaneous applies overwrite each other’s changes. Many current official remote state backends support state locking, including AWS S3, Azure storage, and Google Cloud Storage (GCS). [developer.hashicorp.com]
Without a terraform state lock, two engineers—or developer machine and CI/CD pipeline—might run terraform apply at the same time. Concurrent execution creates severe risks. The second write overwrites the first without knowing about intermediate changes. This causes drifted infrastructure, orphaned cloud resources, or a corrupted state file that fails to map to real-world infrastructure.
How does terraform state lock work with modern S3 backends?
Terraform 1.10, released in November 2024, introduced native state locking directly in S3 using the use_lockfile option. Also, the update eliminated the need for a separate DynamoDB table for S3 backends. AWS added conditional writes to S3 in August 2024, which serves as the underlying technical mechanism enabling this native capability.

Historically, teams relied on a separate DynamoDB table with a LockID attribute to coordinate locks when using Amazon S3 for remote state storage. Older setups required extra infrastructure. However, HashiCorp’s documentation and recent releases have deprecated the dynamodb_table argument for S3 backends as of Terraform 1.11 in February 2025. Configuring a modern S3 backend lets you enable native locking simply by setting use_lockfile = true in your backend block. Doing this removes the maintenance overhead of managing a dedicated table.
You configure a modern S3 backend with native locking using this exact syntax in your configuration files:
terraform {
backend "s3" {
bucket = "my-terraform-state-bucket"
key = "prod/terraform.testate"
region = "us-east-1"
use_lockfile = true
}
}
How terraform state locking works in Azure and other backends
If you use cloud providers other than AWS, your remote backend handles the terraform state lock file differently:
- Azure Blob Storage: Azure uses Blob Leases for state locking. When Terraform runs, it acquires an exclusive lease on the state blob. If another process attempts to write, Azure rejects the request until the lease expires or releases.
- Google Cloud Storage (GCS): GCS uses native object generation numbers and custom metadata locks to prevent concurrent writes.
- Local Backend: Local state files rely on operating system file-system locks. However, local locks do not protect against concurrent runs across different machines or distributed CI/CD runners. These limitations make remote backends mandatory for team environments.
How to unlock terraform state lock when runs crash
If a Terraform run crashes or is interrupted, the state lock can remain active, blocking subsequent operations until you manually release it. You can clear a stuck lock by running the force-unlock command against your workspace.

- Identify the unique lock ID from the error message displayed in your terminal when Terraform fails to acquire the lock.
- Run the force unlock command in your terminal using your specific lock ID:
terraform force-unlock LOCK_ID - Confirm the prompt when Terraform asks if you want to release the lock. You should see a success message indicating that the lock has been released.
Never run this command blindly. Because removing an active lock can lead to severe state corruption, you must verify that no other automated pipeline or team member is currently executing an apply against that state file.
Common problems, edge cases, and who this advice does not suit
You may encounter scenarios where state locks fail or cause friction. So, understanding these edge cases helps you troubleshoot faster:

- Stuck locks from interrupted CI/CD jobs: If a runner times out or gets terminated mid-apply, the lock persists. For example, your recovery path is running
terraform force-unlockmanually or via an administrative pipeline script. - Permission errors on lock tables or lock files: Ensure your IAM role or service principal possesses explicit permissions to create, update, and delete lock files or lease objects in the remote storage bucket. As a result, read-only permissions prevent Terraform from acquiring locks entirely.
Who this advice does not suit: Solo developers working locally with zero shared pipelines find a terraform state lock adds unnecessary infrastructure setup. You can rely on standard local execution. However, the moment a second person or a CI system touches the codebase, remote locking becomes essential.
Related guides
Frequently asked questions
Does Terraform still use DynamoDB?
While DynamoDB was long required for S3 state locking, recent Terraform versions have deprecated the dynamodb_table argument in favor of native S3 locking via use_lockfile = true. HashiCorp has signaled plans to phase out DynamoDB-based locking entirely in future releases.
What does Terraform state mean?
Terraform state is a JSON file that maps your real-world cloud resources to your configuration files, tracking resource metadata and dependencies so Terraform knows what changes to apply during execution.
Why should you avoid disabling state locking?
Disabling locking with the -lock=false flag removes protection against concurrent writes, which can easily corrupt your state file if two operators or pipelines run commands at the same time.
How do you release a stuck terraform state lock?
You release a stuck lock by copying the lock ID from the error output and executing the terraform force-unlock [LOCK_ID] command in your terminal after confirming no other processes are running.