How to fix state blob is already locked in Terraform

TerraformINTERMEDIATEHIGHUpdated Dec 30, 2025· 112 views

This error occurs when Terraform cannot acquire a lock on your Azure Blob Storage state file because another process or operation already holds the lock. It typically happens when multiple pipelines run concurrently or when a previous operation terminated unexpectedly.

Quick fix

Fixes state blob is already locked

# Check your local machine for any running terraform processes
ps aux | grep terraform

# Check Azure Portal for the state blob lease status
# Navigate to Storage Account > Containers > [your-container] > terraform.tfstate
# Look at the 'Leased state' column
  1. Check for active Terraform operations
  2. Break the lease via Azure Portal
  3. Break the lease via Azure CLI
  4. Use terraform force-unlock if lease break fails
terraform — error
# Check your local machine for any running terraform processes
ps aux | grep terraform
# Check Azure Portal for the state blob lease status
# Navigate to Storage Account > Containers > [your-container] > terraform.tfstate
# Look at the 'Leased state' column
state blob is already locked

What this error means

Terraform uses state locking via Azure Blob Storage leases to prevent concurrent modifications that could corrupt your infrastructure state. When one process acquires a lease on the state blob, other processes attempting to read or write the state will fail with this error. This is a safety mechanism to ensure only one Terraform operation modifies the state at a time. The lock persists until the operation completes or is forcefully released.

Typical symptoms

  • Terraform plan or apply commands fail immediately with 'state blob is already locked' error
  • CI/CD pipelines fail at the Terraform step while other pipelines succeed
  • Multiple concurrent deployments conflict and only one succeeds
  • Previous Terraform run crashed or was cancelled but state remains locked for hours
  • Azure Portal shows the state blob with a 'Leased' status that doesn't change

Common causes

  1. A Terraform operation is currently running against the same state file (most common)
  2. A previous Terraform operation crashed, was killed, or timed out without releasing the lock
  3. Multiple CI/CD pipelines, deployments, or team members running Terraform on the same state simultaneously
  4. Network interruption between the Terraform CLI and Azure Blob Storage during a locked operation
  5. Azure lease timeout issues or long-running operations that exceed pipeline timeouts

How to fix "state blob is already locked"

1Check for active Terraform operations

First, verify that no other Terraform operations are currently running against this state file:

bash
# Check your local machine for any running terraform processes
ps aux | grep terraform

# Check Azure Portal for the state blob lease status
# Navigate to Storage Account > Containers > [your-container] > terraform.tfstate
# Look at the 'Leased state' column

If another operation is running locally, wait for it to complete. If it's in a CI/CD pipeline, check your pipeline dashboard to see if a previous run is stuck.

2Break the lease via Azure Portal

If no active operation is running but the state remains locked:

1. Go to the Azure Portal
2. Navigate to your Storage Account
3. Select Containers
4. Open the container holding your state file (usually tfstate or similar)
5. Click on the terraform.tfstate blob
6. In the Overview panel, find the 'Lease state' field showing 'Leased'
7. Click the 'Break lease' button
8. Wait for the status to change to 'Broken'
9. Retry your Terraform operation

3Break the lease via Azure CLI

Alternatively, use the Azure CLI to break the lease programmatically:

bash
az storage blob lease break \
  -b terraform.tfstate \
  -c tfstate \
  --account-name "mystorageaccount" \
  --account-key "your-storage-key"

Replace:
- mystorageaccount with your storage account name
- tfstate with your container name
- Use --connection-string instead of --account-key if you prefer

4Use terraform force-unlock if lease break fails

If breaking the lease doesn't work, use Terraform's force-unlock command. First, identify the lock ID from your error message or by checking recent logs:

bash
terraform force-unlock <LOCK_ID>

The lock ID should be visible in your Terraform error output. Use this only as a last resort since it bypasses safety checks. Be absolutely certain no other operations are using the state file before forcing an unlock.

5Implement lock timeout retry logic

To prevent this error in the future, add lock timeout and retry flags to your Terraform commands:

bash
terraform init -backend-config="..." -lock=true -lock-timeout=120s
terraform plan -lock=true -lock-timeout=120s
terraform apply -lock=true -lock-timeout=120s

This allows Terraform to retry acquiring the lock for up to 120 seconds instead of failing immediately. Adjust the timeout based on your typical operation duration.

6Prevent concurrent pipeline runs

The root cause prevention requires ensuring only one pipeline can run against a state file at a time. Implement one of these approaches:

GitHub Actions:

yaml
concurrency:
  group: terraform-${{ github.ref }}
  cancel-in-progress: false

GitLab CI:

yaml
resource_group: terraform

Azure DevOps:
- Use a pipeline resource lock or manual approval gate
- Enable 'Use the auto-injected checkout task' and set concurrency settings

These prevent multiple pipeline runs from executing Terraform simultaneously on the same state.

Advanced notes

Why locks are critical: Terraform state contains metadata about your infrastructure. Concurrent writes corrupt the state, causing Terraform to lose track of resources, leading to duplicate creation, deletion of in-use resources, or crashes.

Azure Blob Lease mechanism: Azure Blob Storage has a native leasing system. Terraform acquires a lease when starting an operation and releases it on completion. Unlike traditional file locks, leases can become stuck if the process terminates unexpectedly.

Lock timeout behavior: The -lock-timeout flag tells Terraform to retry acquiring the lock every 100ms until the timeout expires. If still locked after the timeout, the operation fails. This is useful for handling transient network issues but won't solve stuck locks from crashed processes.

Terraform 0.14 bug: In Terraform 0.14.8, the Azure backend had a known issue where -lock-timeout was ignored. This was fixed in 0.15.0. If you're on 0.14.x and experiencing persistent lock issues, upgrade to 0.15+ or manually break leases via the Azure Portal.

Disabling locks (not recommended): You can use -lock=false to skip locking entirely, but this is extremely dangerous. Multiple writers can corrupt state, leading to infrastructure inconsistencies. Only disable locks in emergency scenarios with explicit team communication.

State file location: Always store Terraform state in remote backends (Azure Blob Storage, S3, etc.) in production. Local state files can be accidentally committed to git, exposing sensitive data, and provide no locking mechanism for team collaboration.

Frequently asked questions

What does "state blob is already locked" mean?
Terraform uses state locking via Azure Blob Storage leases to prevent concurrent modifications that could corrupt your infrastructure state. When one process acquires a lease on the state blob, other processes attempting to read or write the state will fail with this error. This is a safety mechanism to ensure only one Terraform operation modifies the state at a time.
How do I fix "state blob is already locked"?
This error occurs when Terraform cannot acquire a lock on your Azure Blob Storage state file because another process or operation already holds the lock. It typically happens when multiple pipelines run concurrently or when a previous operation terminated unexpectedly.
What causes "state blob is already locked"?
A Terraform operation is currently running against the same state file (most common) A previous Terraform operation crashed, was killed, or timed out without releasing the lock Multiple CI/CD pipelines, deployments, or team members running Terraform on the same state simultaneously
Is the "state blob is already locked" error serious?
Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.

Official resources & further reading