Keel

How do you lock Terraform state in S3 without DynamoDB?

By the Keel team · Last checked · 4 min read

Short answer

Turn on S3 native state locking by adding use_lockfile = true to your S3 backend. Terraform then writes a lock file next to the state file in the same bucket, so you no longer need a DynamoDB table just for locking. Native locking became generally available in Terraform 1.11.0, and the DynamoDB arguments are deprecated.

What does the S3 backend look like with native locking?

HashiCorp describes use_lockfile as the setting that enables state locking in S3 instead of DynamoDB.

It defaults to false, so you have to turn it on. The lock is stored alongside the state as an object named after the state key with a .tflock suffix.

terraform {
  required_version = ">= 1.11.0"

  backend "s3" {
    bucket       = "your-company-terraform-state"
    key          = "production/app.tfstate"
    region       = "eu-west-2"
    encrypt      = true
    use_lockfile = true
  }
}

With that key, the lock lives at production/app.tfstate.tflock in the same bucket. The bucket name and region are examples; use your own. Pinning required_version stops an older Terraform on someone's laptop from running against the state.

Which Terraform version supports it?

S3 native state locking became generally available in Terraform 1.11.0, released on 27 February 2025, according to HashiCorp's changelog.

That release also deprecated the DynamoDB-related arguments in favour of the new mechanism.

The S3 backend page says DynamoDB-based locking will be removed in a future minor version, so it is worth migrating even if the table works today. OpenTofu's S3 backend has use_lockfile too; its documentation calls it the preferred method, built on S3 conditional writes, and says the OpenTofu team has no plans to deprecate either option. Check the version of every Terraform or OpenTofu binary that touches the state before you change anything.

How do you switch from DynamoDB safely?

Switch in stages, keeping both locking methods on for a short time, so that you can confirm the new lock works before you remove the old one.

HashiCorp's documentation confirms the S3 and DynamoDB arguments can be set together to support migration.

  1. Upgrade Terraform on every laptop and pipeline that runs it, because older versions will not recognise the new argument.
  2. Add use_lockfile = true and keep dynamodb_table for now.
  3. Run terraform init -reconfigure, then a normal plan, and confirm it locks and unlocks cleanly.
  4. Once every pipeline uses the new version, remove dynamodb_table from the backend block.
  5. Delete the DynamoDB table when nothing references it.

Make the change in a pull request like any other, as in Terraform or CDK for a small team, and run the first plan from CI so you see exactly what your pipeline sees.

What permissions does the Terraform role need?

As well as reading and writing the state object, the role that runs Terraform needs permission to create and delete the lock object.

HashiCorp states that with use_lockfile set, s3:GetObject, s3:PutObject and s3:DeleteObject are required on the lock file.

{
  "Effect": "Allow",
  "Action": ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"],
  "Resource": "arn:aws:s3:::your-company-terraform-state/production/app.tfstate.tflock"
}

If locking fails with an access error right after you switch, this is the first thing to check. The state object itself still needs read and write access, and listing the bucket is needed as before. Give those permissions only to the role your pipeline uses, in line with giving a provider AWS access safely.

How does S3 native locking work?

Native locking works by writing a small lock object that can only be created if it does not already exist.

OpenTofu's documentation describes this as using S3 conditional writes with the If-None-Match header, and Terraform's S3 backend uses the same kind of lock object beside the state.

When a run starts, it tries to create the lock. If another run holds it, the write is refused and your run waits or exits with a lock error. When the run finishes, the lock object is deleted. There is no second service to provision, monitor or grant access to, which is the whole saving.

What should you do about a stuck lock?

Check before you force anything.

A stuck lock usually means a run crashed or was cancelled, but it can also mean someone else is genuinely applying a change right now.

  1. Confirm no pipeline or person is running Terraform against that state.
  2. Read the lock error, which shows the lock ID and who took it.
  3. Run terraform force-unlock <LOCK_ID>.
  4. Run a fresh plan and check nothing unexpected has changed.

Never remove locks routinely to make things work. Two runs writing the same state at once can corrupt it, and that is far more expensive than waiting a few minutes.

Which bucket settings should you check?

Check that the bucket is built to protect state, because state files can hold sensitive values and losing one is painful.

  • Versioning on, so you can recover an earlier state file.
  • Encryption on, matching the encrypt = true setting above.
  • Public access blocked.
  • Access limited to the roles that run Terraform, ideally in a separate account.
  • A separate state key per environment, so staging runs cannot touch production state.

For production, keeping state in a dedicated account means a compromised application role cannot read or tamper with it. If you are unsure how to split accounts, read how many AWS accounts a startup needs.

Is your Terraform state safe?

Tick each item that is true today.

  • State lives in a remote backend with locking on.
  • Every machine and pipeline runs a Terraform version that supports use_lockfile.
  • The Terraform role can create and delete the lock object.
  • The state bucket has versioning and encryption on and public access blocked.
  • Each environment has its own state key.
  • Nobody removes locks without checking that no run is active.

Decision point: Five or six ticks: state is well protected. Three or fewer: one crashed run could leave you with corrupted or unrecoverable state.

Sources

  1. HashiCorp, Terraform S3 backend (checked November 2026)
  2. HashiCorp, Terraform 1.11 changelog (27 February 2025)
  3. OpenTofu docs, S3 backend (checked November 2026)

Keep reading

Keel is an AWS release and recovery operations service for SaaS teams of 3 to 15 developers, provided by Bargainn Services Ltd, a UK company registered at Companies House (16497746). Registered office: 3rd Floor, 86-90 Paul Street, London, EC2A 4NE, United Kingdom.