Email copiado — support@tuurt.com
Cargando experiencia
devops · October 20, 2026 · 6 min

Terraform State Locking Across Multiple AWS Pipelines: What Happens When Two Applies Collide

Two CI pipelines fired terraform apply almost at once. Here is how the S3 and DynamoDB lock works, what happens when it collides, and what we changed so two applies can never overlap.

By Tuurt Team

Terraform State Locking Across Multiple AWS Pipelines: What Happens When Two Applies Collide

We run several CI pipelines that can trigger a terraform apply against the same workspace: one on merge to main, a manual one for infrastructure hotfixes, and another that runs on a cron to catch drift. As long as each one fires at a different moment, nothing goes wrong. The problem shows up the day two of them run against the same state almost at once, because at that point it stops being a matter of luck and becomes a question of what locking mechanism is actually configured, and how well we understand it.

How the lock works with an S3 backend

With the s3 backend configured alongside a dynamodb_table, Terraform doesn't lock anything by itself: it delegates the lock to a DynamoDB table. Before reading or writing the state, Terraform attempts a conditional PutItem against that table, using a LockID partition key built from the bucket and the state file path (<bucket>/<key>). If the item doesn't exist yet, the PutItem succeeds and the lock is held. If it already exists -because another process grabbed it first- the condition fails with a ConditionalCheckFailedException, which Terraform surfaces as the error most of us eventually learn to recognize:

Error acquiring the state lock

Error message: ConditionalCheckFailedException: The conditional
request failed
Lock Info:
  ID:        3f2a1c9e-...
  Path:      s3://tuurt-tfstate/prod/terraform.tfstate
  Operation: OperationTypeApply
  Who:       ci-runner@github-actions
  Created:   ...

This isn't a failure of the system, it's the system working exactly as designed: two processes tried to take the same lock, one wins, and the other gets a clear, immediate error instead of silently corrupting the state by writing over it.

The detail that isn't obvious: plan locks too

What caught us off guard the first time we actually hit this is that it doesn't take two applies colliding. A terraform plan also acquires the lock by default, because computing a consistent plan requires reading the state while no other process is modifying it. So the real-world scenario isn't just "two simultaneous applies": it's a drift-detection pipeline running plan on a schedule, and a deploy pipeline attempting an apply right in the window where the first one holds the lock. The second pipeline fails with the same ConditionalCheckFailedException, even though conceptually neither of them was changing anything in conflict with the other.

Why letting the lock fail isn't a fix

The instinctive reaction when a pipeline fails for this reason is to just retry it by hand. That works once. It isn't a fix because it doesn't remove the cause: if two pipelines can fire in the same time window against the same workspace, they'll collide again, and every collision is a red pipeline someone has to diagnose just to confirm it isn't a real problem before relaunching it.

There's an even worse escape hatch we've seen suggested more than once: using terraform force-unlock to manually release the lock when a pipeline looks "stuck." The issue is that force-unlock never checks whether the process that held the lock actually finished; it just deletes the item from DynamoDB. If we run it while the other apply is still in flight, we've just removed the one protection that was preventing two processes from writing the state at the same time -exactly the scenario the lock exists to prevent. force-unlock has a legitimate use -a runner that died mid-apply and left an orphaned lock behind- but it should never be the automatic response, or the first reflex, to a lock error.

What we changed: making sure two applies can never overlap, before they ever reach DynamoDB

The DynamoDB lock is the last line of defense, not the first. Relying on the lock API to resolve concurrency means the first symptom of a pipeline design problem is a red pipeline in production. What we moved was concurrency control one level up, into the CI orchestrator itself, so that two runs against the same workspace never even attempt the lock at the same time.

In GitHub Actions, this is solved with a concurrency group per workspace:

concurrency:
  group: terraform-apply-${{ inputs.workspace }}
  cancel-in-progress: false

The part that matters is cancel-in-progress: false. The default in a lot of examples is true, meant for cancelling stale PR builds when a new push lands, but applied to an apply it's dangerous: cancelling an apply midway can leave resources half-created and the state out of sync with the real infrastructure. What we want for Terraform isn't to cancel the one that's running, it's to queue the one that arrives after it, so it runs immediately once the first one finishes.

In GitLab CI the equivalent is a resource_group:

deploy_infra:
  resource_group: terraform-prod
  script:
    - terraform apply -auto-approve

A resource_group guarantees that, out of every job declaring the same group, only one runs at a time; the rest queue up in the order they arrived.

With this in place, the drift-detection pipeline and the deploy pipeline share the same concurrency group per workspace. The DynamoDB lock stays configured and is still necessary -it covers the case of someone running terraform apply from their own machine, outside the pipeline- but it stops being the mechanism that normally decides execution order. That job now belongs to the CI orchestrator, which also gives us a visible queue instead of a string of red errors that have to be relaunched by hand.

What we check before signing off on a Terraform pipeline with multiple triggers

Before accepting that a Terraform workspace is safe against concurrent runs, we confirm the backend has a DynamoDB table configured for locking, not just an S3 bucket for the state. We confirm that every pipeline that can touch that workspace -deploy, drift detection, manual runs- shares the same concurrency group in the CI orchestrator, keyed by workspace rather than by repository or branch, because two different workspaces can and should run in parallel. We check that no automatic cancellation step can interrupt an apply in progress. And we leave it documented, for whoever operates the pipeline, that force-unlock is used only after confirming the process that held the lock no longer exists, never as the first step to "unstick" a pipeline that looks hung.

terraform aws devops ci-cd
← Back to blog