AWS

5 Mins Read

State Locking with S3 Without DynamoDB in Terraform

Voiced by Amazon Polly

Introduction

When using Terraform in a team environment, ensuring that multiple people don’t modify the state simultaneously is crucial. Typically, Terraform provides state locking via Amazon S3 and DynamoDB. However, some users might prefer not to use DynamoDB due to cost, complexity, or organizational constraints. This blog explores how to implement state locking using S3 alone, discussing its benefits, limitations, and possible workarounds.

Why Terraform State Locking?

State locking prevents concurrent modifications to the Terraform state, which can lead to corruption and inconsistencies. Without locking, multiple users applying changes simultaneously can overwrite each other’s updates, leading to an unpredictable infrastructure state.

Default Approach: S3 + DynamoDB

The recommended approach for state locking with AWS is:

  • S3: Stores the Terraform state file.
  • DynamoDB: Provides locking to prevent concurrent operations.

DynamoDB achieves this by storing a lock item that prevents another operation from proceeding until the first one is completed.

Using S3 Without DynamoDB

Recent Announcement

Terraform has recently introduced native state locking in S3, removing the need for DynamoDB. This enhancement simplifies the setup, reduces costs, and eliminates the need for additional AWS services. Now, teams can leverage S3 for both state storage and locking without relying on external databases.

Enabling S3 Native Locking

To enable S3’s built-in locking, simply set use_lockfile to true in your backend configuration.

Before: Using DynamoDB for Locking

After: Switching to S3 Native Locking

Enhance Your Productivity with Microsoft Copilot

  • Effortless Integration
  • AI-Powered Assistance
Get Started Now

How It Works

With S3 locking enabled, Terraform creates a lock file in the same location as the state file. This lock file shares the same name as the state file but has a “.tflock” extension. Depending on your setup, you may need to update S3 bucket policies and IAM permissions to accommodate the new lock file.

Example Configuration

Example 1: Deploying an EC2 Instance

  1. Create the configuration files.

  1. Initialize the directory with terraform init.

  1. Run the terraform plan command to view the plan.

 

  1. Run the terraform apply command to create the resources.

  1. Check the Statefile and the lockfile in the S3 bucket.

  1. When you try to perform or run the terraform commands simultaneously, you will the get the message “Error acquiring the state lock”. Which means the state lock is enabled without the use of DynamoDB.

 

Example 2: Creating an S3 Bucket with State Locking

  1. Write the configuration files.

  1. Initialize the directory with terraform init

  1. Run the terraform plan command to see the plan.

  1. Run the terraform apply command to execute the changes.

  1. Check the S3 bucket for the state file and the lock file.

  1. When you try to perform or run the terraform commands simultaneously, you will the get the message “Error acquiring the state lock”. Which means the state lock is enabled without the use of dynamodb.

Conclusion

With the introduction of S3 native state locking, terraform users can now avoid the additional complexity and cost of using DynamoDB. This simplifies the infrastructure while ensuring safe and reliable state management. If you’re currently using DynamoDB, consider migrating to the new S3-based locking mechanism to take advantage of this feature.

Become an Azure Expert in Just 2 Months with Industry-Certified Trainers

  • Career-Boosting Skills
  • Hands-on Labs
  • Flexible Learning
Enroll Now

About CloudThat

CloudThat is a leading provider of Cloud Training and Consulting services with a global presence in India, the USA, Asia, Europe, and Africa. Specializing in AWS, Microsoft Azure, GCP, VMware, Databricks, and more, the company serves mid-market and enterprise clients, offering comprehensive expertise in Cloud Migration, Data Platforms, DevOps, IoT, AI/ML, and more.

CloudThat is the first Indian Company to win the prestigious Microsoft Partner 2024 Award and is recognized as a top-tier partner with AWS and Microsoft, including the prestigious ‘Think Big’ partner award from AWS and the Microsoft Superstars FY 2023 award in Asia & India. Having trained 650k+ professionals in 500+ cloud certifications and completed 300+ consulting projects globally, CloudThat is an official AWS Advanced Consulting Partner, Microsoft Gold Partner, AWS Training PartnerAWS Migration PartnerAWS Data and Analytics PartnerAWS DevOps Competency PartnerAWS GenAI Competency PartnerAmazon QuickSight Service Delivery PartnerAmazon EKS Service Delivery Partner AWS Microsoft Workload PartnersAmazon EC2 Service Delivery PartnerAmazon ECS Service Delivery PartnerAWS Glue Service Delivery PartnerAmazon Redshift Service Delivery PartnerAWS Control Tower Service Delivery PartnerAWS WAF Service Delivery PartnerAmazon CloudFrontAmazon OpenSearchAWS DMS and many more.

To get started, go through our Consultancy page and Managed Services PackageCloudThat’s offerings.

WRITTEN BY Sruti Samatkar

Share

Comments

    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!