Skip to content

This repository demonstrates the use of native state locking for S3 backends introduced in Terraform v1.10. With the use_lockfile = true option, Terraform now manages .tflock files directly in the S3 bucket β€” eliminating the need for a DynamoDB table for state locking.

Notifications You must be signed in to change notification settings

AkshaySiv/terraform-s3-locking-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Terraform S3 Native Locking Demo

This project demonstrates the use of Terraform v1.10+ with native S3 backend locking, a new feature that eliminates the need for a DynamoDB table when managing Terraform state.

πŸš€ What's New?

Terraform now supports native .tflock files in S3. By enabling use_lockfile = true in your backend configuration, Terraform will automatically manage state locks using S3 β€” no external lock tables needed.

πŸ“ Project Structure

.
β”œβ”€β”€ main.tf          # Creates a sample S3 bucket
β”œβ”€β”€ backend.tf       # Configures S3 backend with native locking
β”œβ”€β”€ variables.tf     # Input variables for region and bucket name
└── outputs.tf       # (Optional) Outputs to show bucket details

πŸ› οΈ Prerequisites

  • Terraform v1.10 or later
  • S3 bucket must exist (Terraform backend cannot create it)

🌐 Backend Configuration (backend.tf)

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

βœ… Make sure the S3 bucket is created and versioning is enabled before initializing.

πŸͺ£ Sample Resource (main.tf)

resource "aws_s3_bucket" "demo" {
  bucket = var.bucket_name

  tags = {
    Name        = "Terraform S3 Demo"
    Environment = "Dev"
  }
}

πŸ“¦ Initialize & Apply

terraform init
terraform apply

alt text

During apply, you’ll see a .tflock file appear in your S3 bucket. It disappears automatically when the plan/apply completes

alt text

πŸ” Benefits of Native Locking

  • No need for DynamoDB
  • Easier setup and maintenance
  • Saved cost by removing DynamoDB

πŸ“˜ References


About

This repository demonstrates the use of native state locking for S3 backends introduced in Terraform v1.10. With the use_lockfile = true option, Terraform now manages .tflock files directly in the S3 bucket β€” eliminating the need for a DynamoDB table for state locking.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages