Sitemap

Native S3 Backend Locking in Terraform: A Paradigm Shift from DynamoDB

3 min readMay 15, 2025

--

A significant enhancement in Terraform’s state management capabilities: the introduction of native state locking within the S3 backend. This feature, currently available as an opt-in experimental capability starting with Terraform 1.9.0 but with Terraform version 1.12 , this seems a stable feature now. This has fundamentally changes how state locking is handled in AWS environments by reducing operational complexity and cost.

Background: The Role of Locking in Terraform State Management

Terraform’s state file is the single source of truth for infrastructure resources. When multiple operators or automation pipelines interact with the same state file, concurrent modifications can lead to race conditions, corrupted state, and ultimately, infrastructure drift or failure. To mitigate this, Terraform employs state locking mechanisms that serialize access to the state.
Historically, when using AWS S3 as a remote backend, Terraform has relied on DynamoDB tables to implement state locking. This approach, while robust and battle-tested, introduces an additional AWS resource dependency:

  • Operational overhead: Managing a dedicated DynamoDB table for locks.
  • Cost implications: DynamoDB incurs charges based on provisioned throughput and storage.
  • Complexity: Additional IAM permissions and policies are required to secure and maintain the lock table.

Introducing S3 Native State Locking

Terraform’s new S3 native locking mechanism eliminates the need for an external DynamoDB table by leveraging S3 itself to manage locks. This is achieved through the creation of a lock file alongside the state file, suffixed with .tflock. The key advantages include:

  • Simplified infrastructure: No need to provision or maintain a DynamoDB table.
  • Reduced cost: Eliminates DynamoDB charges associated with locking.
  • Backward compatibility: Supports concurrent use with DynamoDB locks for redundancy during migration phases.
  • Improved user experience: Streamlines backend configuration and reduces IAM policy complexity.

Enabling S3 Native Locking

To adopt native S3 locking, ensure you are running Terraform version 1.9.0 or higher. The configuration change is minimal and involves setting the use_lockfile = true attribute to in your S3 backend configuration.

Legacy Configuration Using DynamoDB Locking

 terraform {
backend "s3" {
bucket = "your-terraform-state-bucket"
key = "path/to/your/statefile.tfstate"
region = "us-east-1"
dynamodb_table = "your-dynamodb-lock-table" # Existing DynamoDB lock
encrypt = true
}
}

Updated Configuration Using S3 Native Locking

terraform {
backend "s3" {
bucket = "your-terraform-state-bucket"
key = "path/to/your/statefile.tfstate"
region = "us-east-1"
encrypt = true
use_lockfile = true # Enable native S3 locking
}
}

How Native Locking Works Under the Hood

When enabled, Terraform creates a lock file in the S3 bucket at the same path as the state file but with a .lockfile extension. This file acts as a semaphore, signaling that the state is currently locked. Terraform will attempt to acquire this lock file before performing any state operations, ensuring serialized access.
Important: To support this new locking mechanism, you may need to update your S3 bucket policies and IAM roles to grant permissions for managing .tflock files alongside your state files.

Example IAM Policy Snippet for S3 Locking

{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:PutObject",
"s3:DeleteObject"
],
"Resource": [
"arn:aws:s3:::your-terraform-state-bucket/path/to/your/*.tfstate",
"arn:aws:s3:::your-terraform-state-bucket/path/to/your/*.tflock"
]
}

Transition Strategy and Coexistence

Terraform supports simultaneous use of both locking mechanisms. When both dynamodb_table and use_lockfile are configured, Terraform requires successful acquisition of locks from both sources before proceeding. This dual-locking mode facilitates a smooth migration path, allowing teams to validate native locking without risking state corruption.

Roadmap and Future Considerations

The use_lockfile attribute is currently marked as experimental but is slated for full support in an upcoming minor Terraform release. In terraform version 1.12Terraform has deprecate DynamoDB-based locking attributes, signaling a shift towards a simplified, native locking model.
This evolution reflects Terraform’s commitment to reducing operational complexity and improving the user experience in managing remote state, particularly for AWS users.

--

--

Manish Sharma
Manish Sharma

Written by Manish Sharma

I am a technology enthusiast with a passion for continuous learning & innovation. Certified as an AWS Soln Architect Associate & HashiCorp Terraform Associate.

Responses (1)