Terraform Academy

Infrastructure operations

Terraform S3 state locking: What changes with native lockfiles?

Terraform Academy editorial team · Reviewed

A state lock is a concurrency control, not a backup, permission boundary or substitute for a deployment approval. The purpose is to coordinate state-writing operations so that two runs do not independently overwrite the same record of managed infrastructure.

What native S3 locking means

The Terraform S3 backend supports an opt-in lockfile through the use_lockfile setting. HashiCorp's backend reference marks DynamoDB-based locking as deprecated. This does not mean every existing team should immediately remove its DynamoDB table: deployed Terraform versions, automation clients, permissions and rollback requirements determine the safe transition.

Illustrative backend configuration

This example is documentation only. It assumes an existing authorized S3 bucket, a compatible Terraform version, and reviewed backend permissions. The bucket name is a placeholder. No cloud resources are created by this example.

terraform {
  backend "s3" {
    bucket       = "example-state-bucket"
    key          = "training/terraform.tfstate"
    region       = "us-east-1"
    encrypt      = true
    use_lockfile = true
  }
}

Use the official S3 backend documentation to review the state-object and lock-object permissions separately. Enable bucket versioning, restrict access to state and backups, and use approved workload credentials rather than embedding secrets in backend configuration.

Questions to resolve before migration

  1. Which Terraform versions and automation clients access this state? Are all compatible with the intended locking mechanism?
  2. Does every writer coordinate on the same backend and lock strategy? During a compatible transition, the S3 backend documentation permits both locking settings.
  3. Are state versions recoverable, permissions scoped correctly, and concurrent jobs controlled in a nonproduction rehearsal?
  4. Has the team agreed on a rollback procedure before retiring any existing lock infrastructure?

When a lock already exists

Investigate the owning run first. A lock can indicate another active operation, not a fault. Coordinate with the team and inspect the automation run status before considering recovery. Do not routinely disable locking or remove a lock owned by another process. HashiCorp documents manual unlock as a recovery mechanism for failed unlocks, not as a shortcut around concurrency.

Practice scenario

Two CI jobs target the same environment. One is still running while the other reports a lock conflict. Explain why the appropriate response begins with checking the first job, identifying the shared backend and preventing overlapping state writes. What evidence would distinguish an active lock from an abandoned one?

Next, explore backend, cloud infrastructure and configuration exercises in the lab catalog. Review changes in a disposable learning environment rather than applying examples to production.

Explore infrastructure labs

Primary references

Related learning guides