๐ Terraform S3 Native Locking Demo


Managing Terraform state securely and efficiently just got easier! With the release of Terraform v1.10+, you can now leverage native state locking using .tflock
files directly in Amazon S3 โ no more setting up a DynamoDB table.
๐ฏ What's New?
Terraform now supports native .tflock
files in S3. By simply adding use_lockfile = true
to your backend configuration, Terraform handles locking natively:
No DynamoDB. No extra infra. Just a cleaner and simpler workflow.
๐ GitHub Repo: AkshaySiv/terraform-s3-locking-demo
๐ Project Structure
bashCopyEdit.
โโโ main.tf # Creates a sample S3 bucket
โโโ backend.tf # S3 backend config with native locking enabled
โโโ variables.tf # Input variables (region, bucket name)
โโโ outputs.tf # (Optional) Outputs to display bucket details
๐ ๏ธ Prerequisites
โ Terraform v1.10+
โ S3 bucket must exist beforehand
โ Versioning must be enabled on the S3 bucket (recommended)
Backend Configuration (backend.tf
)
Here, we're configuring the Terraform backend to use S3 for state storage with native locking enabled use_lockfile = true
Note: The specified S3 bucket must be created manually beforehand โ Terraform will not create it for you.
terraform {
backend "s3" {
bucket = "akshay-my-terraform-state-bucket"
key = "state/terraform.tfstate"
region = "us-east-1"
encrypt = true
use_lockfile = true
}
}
๐ชฃ Sample Resource (main.tf
)
This Terraform block creates an AWS S3 bucket named from the bucket_name
variable and tags it with "Name=Terraform S3 Demo"
and "Environment=Dev"
resource "aws_s3_bucket" "demo" {
bucket = var.bucket_name
tags = {
Name = "Terraform S3 Demo"
Environment = "Dev"
}
}
โ๏ธ Initialize & Apply
terraform init
terraform apply
During apply
, youโll notice a .tflock
file pop up in your S3 bucket โ this file represents the state lock. It will disappear automatically once the plan/apply is complete.
๐ Why Native Locking Rocks
๐ซ No DynamoDB table required
โก Simpler setup
๐ฐ Reduced cost
๐งโโ๏ธ Less maintenance overhead
๐ References
Subscribe to my newsletter
Read articles from AKSHAY SIV directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

AKSHAY SIV
AKSHAY SIV
๐ DevOps Engineer | Cloud Enthusiast | Automation Specialist ๐ Sharing insights on DevOps best practices, infrastructure as code, and system reliability.