"Error acquiring the state lock" means another Terraform process holds the terraform state lock for this state file, or a previous run died and never released it. Don't reach for -lock=false. Read the Lock Info block in the error (who, which operation, when), confirm that process is really gone, and only then run terraform force-unlock <ID>. On the S3 backend, switch to native locking with use_lockfile = true, and in CI stop two runs touching the same state at once.

I reproduced the error with Terraform 1.17.1 on a local backend so the outputs below are real, and checked backend behaviour against the HashiCorp docs and the Terraform source as of December 2026.

What the Terraform State Lock Error Looks Like

Here is the full error from a second terraform plan started while an apply was running in another shell (hostname redacted):

Error: Error acquiring the state lock

Error message: resource temporarily unavailable
Lock Info:
  ID:        e409bf58-76d3-4997-6754-01af7f39648d
  Path:      terraform.tfstate
  Operation: OperationTypeApply
  Who:       you@laptop.local
  Version:   1.17.1
  Created:   2026-12-02 02:52:13.41205 +0000 UTC
  Info:      


Terraform acquires a state lock to protect the state from being written
by multiple users at the same time. Please resolve the issue above and try
again. For most commands, you can disable locking with the "-lock=false"
flag, but this is not recommended.

Every field matters:

  • ID is the nonce you need for force-unlock. The locking docs explain that it exists so an unlock targets exactly this lock and not one taken later.
  • Path is the state being locked. On S3 it shows bucket/key, which tells you which environment is stuck.
  • Operation tells you what the holder was doing. OperationTypeApply is the one to be careful with; OperationTypePlan is much safer to clear.
  • Who is user@host. In CI this is the runner's user and hostname, which you can match to a job.
  • Created lets you match the lock to a CI run or a person's terminal session by time.

Note that plan was the command that failed. Terraform takes the lock for operations that could write state, and it takes it for plan too, so it reads a consistent snapshot while refreshing resources against it. A team running plans on every pull request can block an apply just as easily as another apply can.

What Terraform Locks and Which Backends Support It

Locking is per state file, and whether you get it at all depends on the backend. From the HashiCorp backend docs:

Backend Locking How it works force-unlock
local Yes OS file lock on the state file, plus a .terraform.tfstate.lock.info file Not from another process
s3 Opt-in use_lockfile = true writes a .tflock object; DynamoDB table is deprecated Yes
gcs Yes Lock object next to the state Yes
azurerm Yes Azure Blob Storage native capabilities (blob lease) Yes
pg Yes Postgres advisory locks Not supported; lock ends with the session
kubernetes Yes A Lease resource Yes
http Optional LOCK/UNLOCK requests; 423 or 409 when held Yes
HCP Terraform Yes Workspace lock and run queue Via workspace UI/API

Two details people miss. First, S3 locking is opt-in: the S3 backend docs say so explicitly. An S3 backend with neither use_lockfile nor dynamodb_table has no locking at all, and two applies can silently overwrite each other's state. Second, the pg backend's advisory locks release automatically when the session dies, which is why force-unlock isn't supported there.

Reproducing the Lock on a Local Backend

You can see the mechanics without any cloud account. This configuration holds the lock for 60 seconds on every apply:

terraform {
  required_version = ">= 1.10"
}

# Recreated on every apply; the provisioner keeps the run (and the lock) open.
resource "terraform_data" "slow" {
  triggers_replace = timestamp()

  provisioner "local-exec" {
    command = "sleep 60"
  }
}
terraform init
terraform apply -auto-approve &      # shell 1, holds the lock for ~60s
sleep 5
cat .terraform.tfstate.lock.info     # the lock metadata, as JSON
terraform plan                       # shell 2: Error acquiring the state lock (exit 1)
terraform plan -lock-timeout=5s      # retries for 5s, then the same error

The lock file is plain JSON:

{"ID":"e409bf58-76d3-4997-6754-01af7f39648d","Operation":"OperationTypeApply","Info":"","Who":"you@laptop.local","Version":"1.17.1","Created":"2026-12-02T02:52:13.41205Z","Path":"terraform.tfstate"}

Pipe it through jq or paste it into a JSON formatter if you want it readable. With -lock-timeout, Terraform first prints Acquiring state lock. This may take a few moments... and keeps retrying until the timeout, which is what you want in CI.

Two results surprised colleagues when I showed them:

  1. force-unlock does nothing useful on a local backend held by a live process. Running terraform force-unlock -force <ID> from another shell returned Failed to unlock state: LocalState not locked and exit code 1. The force-unlock docs say it directly: local state files cannot be unlocked by another process.
  2. Killing the process releases a local lock. After kill -9 on the apply, the .terraform.tfstate.lock.info file was still on disk, but the next plan ran fine. The real lock is held by the operating system and dies with the process; the leftover info file is just metadata.

So for local state: find the process, let it finish or stop it, and move on. The rest of this guide is about remote backends, where a lock can outlive the process that took it.

S3 Native Locking With use_lockfile

S3 is where most stale locks happen, and it changed recently. The timeline, from the Terraform changelog:

  • Terraform 1.10 added S3 native state locking.
  • Terraform 1.11 made it generally available and deprecated the DynamoDB arguments (dynamodb_table, dynamodb_endpoint).
  • As of Terraform 1.17, the S3 backend docs still describe DynamoDB locking as "deprecated and will be removed in a future minor version". It works today, but don't build anything new on it.

The mechanism is simple. In the Terraform 1.17.1 S3 client, locking is a PutObject of the lock info JSON to <key>.tflock with If-None-Match: *. S3's conditional write succeeds only if the object doesn't exist yet, so exactly one client wins. Unlock deletes the object.

terraform {
  backend "s3" {
    bucket       = "acme-terraform-state"
    key          = "prod/network/terraform.tfstate"
    region       = "eu-west-1"
    encrypt      = true
    use_lockfile = true
  }
}

The IAM policy needs s3:GetObject, s3:PutObject and s3:DeleteObject on the lock object as well as the state (if init or the lock write fails with a 403, see our S3 Access Denied troubleshooting guide):

{
  "Effect": "Allow",
  "Action": ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"],
  "Resource": [
    "arn:aws:s3:::acme-terraform-state/prod/network/terraform.tfstate.tflock"
  ]
}

A missing DeleteObject gives you a classic stale-lock factory: the lock is acquired, the run succeeds, the unlock fails, and the next run is blocked.

Migrating from DynamoDB locking

Do it in two steps so there is never a window without a lock:

  1. Add use_lockfile = true alongside dynamodb_table. With both set, the S3 client takes the file lock first, then the DynamoDB lock, and both must succeed (if DynamoDB fails, it releases the file lock).
  2. Make sure every pipeline and laptop is on Terraform 1.11 or later. Run with both for a while.
  3. Remove dynamodb_table, run terraform init -reconfigure, and delete the table once nothing uses it.

Skipping step 2 is the trap: an old Terraform binary that only knows DynamoDB will happily ignore the .tflock file.

Inspecting a stale S3 lock

The lock is a normal object, so you can read it before deciding anything:

aws s3 cp s3://acme-terraform-state/prod/network/terraform.tfstate.tflock - | jq .
aws s3api head-object --bucket acme-terraform-state \
  --key prod/network/terraform.tfstate --query LastModified

If you have bucket versioning on (you should), the OpenTofu S3 docs make a fair point that applies to Terraform too: every lock and unlock creates object versions, so add a lifecycle rule to expire noncurrent versions of *.tflock objects.

How to Force-Unlock Terraform State Safely

terraform force-unlock removes the lock record. It does not touch infrastructure. The danger is entirely in unlocking while someone is still writing. The locking docs are blunt: it should only be used to unlock your own lock when automatic unlocking failed.

Here is the procedure I use:

  1. Read the lock. Note Who, Operation and Created from the error or the .tflock object.
  2. Find the holder. Match Who and Created to a CI job or a person. Check the CI system for running or recently cancelled jobs on that state. Message the person if it was a laptop.
  3. Confirm the holder is dead. A job marked "cancelled" in the UI may still be running on a self-hosted runner. Check the runner, not just the dashboard.
  4. Check what it was doing. If Operation is OperationTypeApply, assume it may have created or changed resources and possibly not written all of them to state.
  5. Unlock with the exact ID.

bash terraform force-unlock e409bf58-76d3-4997-6754-01af7f39648d # add -force in automation to skip the confirmation prompt

  1. Plan immediately. Run terraform plan and read it carefully. Resources created by the dead apply but never recorded will show as "to be created" again and may then fail with "already exists". Bring those in with import blocks or terraform import rather than letting Terraform create duplicates.
  2. Write down why it happened. Nearly every stale lock traces back to a cancelled job, a killed runner, or missing unlock permissions. Fix that cause.

What not to do: never run apply -lock=false to get past a lock. If the holder is alive, you now have two writers and the last one to upload state wins, dropping the other's resources from tracking.

Prevent State Lock Conflicts in CI

Most lock errors in teams are not stale locks. They are two pipelines legitimately colliding. Fix that with queueing, not unlocking.

Use -lock-timeout

Give every CI command a -lock-timeout so a short overlap waits instead of failing:

terraform plan  -lock-timeout=10m -out=tfplan
terraform apply -lock-timeout=10m tfplan

GitHub Actions: concurrency groups

Serialize runs per state with a concurrency group. Leave cancel-in-progress false for anything that applies:

name: terraform-prod-network
on:
  push:
    branches: [main]
    paths: ["infra/prod/network/**"]

concurrency:
  group: tfstate-prod-network
  cancel-in-progress: false
  queue: max

permissions:
  contents: read
  id-token: write

jobs:
  apply:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: infra/prod/network
    steps:
      - uses: actions/checkout@v7
      - uses: hashicorp/setup-terraform@v4
        with:
          terraform_version: 1.17.2
      - run: terraform init -input=false
      - run: terraform apply -input=false -auto-approve -lock-timeout=10m

By default a concurrency group keeps one running and one pending run, and a newer pending run cancels the older pending one. The queue: max option lets up to 100 runs wait in order instead. Why avoid cancel-in-progress on applies? GitHub's cancellation reference says the runner sends SIGINT, waits 7.5 seconds, sends SIGTERM, waits 2.5 seconds, then kills the process tree. Terraform handles SIGINT by trying to stop gracefully, but a resource mid-creation can easily take longer than ten seconds, and then the lock is left behind. That's also where OIDC credentials expiring mid-apply can bite; see our guide to GitHub Actions OIDC for AWS, GCP and Azure for session duration settings.

GitLab CI: resource_group

GitLab's resource_group keyword makes jobs in the same group run one at a time:

apply:prod-network:
  stage: deploy
  image:
    name: hashicorp/terraform:1.17.2
    entrypoint: [""]
  resource_group: tfstate-prod-network
  script:
    - cd infra/prod/network
    - terraform init -input=false
    - terraform apply -input=false -auto-approve -lock-timeout=10m
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

Split state to shrink the blast radius

If one state file covers everything, every pull request plan contends for the same lock. Splitting by environment and component (network, data, apps) means fewer collisions and smaller blast radius when a lock does go stale.

OpenTofu State Locking Differences

OpenTofu forked from Terraform 1.5 and has developed its S3 locking on its own track. Per the OpenTofu S3 backend docs:

  • OpenTofu also supports use_lockfile = true with S3 conditional writes; the OpenTofu 1.10 changelog lists S3 locking without DynamoDB.
  • Unlike Terraform, OpenTofu states that both S3 and DynamoDB locking are fully supported and it has no plans to deprecate either.
  • The migration path is the same: set both, run for a while, then drop dynamodb_table.
  • The commands are the same with a different binary name: tofu force-unlock <ID>, -lock-timeout, -lock=false.

If you run both tools against the same bucket during a migration, make sure both are configured with the same locking method, or they won't see each other's locks. Our OpenTofu vs Terraform migration guide covers the wider differences, and if you're still choosing a tool at all, the trade-offs in Pulumi vs Terraform include how each handles state.

Frequently Asked Questions

Is it safe to run terraform force-unlock?

It is safe when the process holding the lock is definitely gone. The command only deletes the lock record and doesn't modify infrastructure. It is unsafe while the holder is still running, because two processes can then write state and one will overwrite the other. Always confirm the holder first and run terraform plan straight after.

Why does terraform plan take a state lock?

Terraform locks state for plan by default so the plan is computed from a consistent snapshot while it refreshes resources. That's why a running plan can block an apply, as the reproduction above shows. Use -lock-timeout in CI so short overlaps wait instead of failing, or -lock=false only for plans you will throw away and never apply.

Is DynamoDB state locking deprecated in Terraform?

Yes. Terraform 1.11 made S3 native locking (use_lockfile = true) generally available and deprecated the DynamoDB arguments, and the Terraform 1.17 S3 docs say DynamoDB locking will be removed in a future minor version. OpenTofu, by contrast, says it has no plans to deprecate DynamoDB locking.

Where is the S3 lock file stored?

Next to the state object, with .tflock appended to the key. For a state at prod/network/terraform.tfstate the lock is prod/network/terraform.tfstate.tflock. It contains the same JSON lock info shown in the error, so you can read it with aws s3 cp ... -.

What does "resource temporarily unavailable" mean in the lock error?

On the local backend it is the operating system's message for a file lock that another process already holds. It is not a disk or permissions problem. Find the other Terraform process; when it exits, the lock is released even if a .lock.info file is left behind.

Conclusion

The terraform state lock error is protection working, not a bug. Read the lock info, find and confirm the holder is gone, then force-unlock with the exact ID and plan straight away to catch half-finished applies. On S3, move to use_lockfile = true with the right IAM permissions before DynamoDB locking is removed. And stop most lock errors from happening at all by queueing CI runs per state with concurrency groups or resource_group, adding -lock-timeout, and never cancelling an apply mid-flight.