Terraform state locking using DynamoDB

provider "aws" {
region = "us-east-1"
access_key = var.access_key
secret_key = var.secret_key
}
provider "aws" {
region = "us-east-1"
access_key = var.access_key
secret_key = var.secret_key
}
resource "aws_instance" "ec2_example" {
ami = "ami-0767046d1677be5a0"
instance_type = "t2.micro"
tags = {
Name = "EC2 Instance with remote state"
}
}
provider "aws" {
region = "us-east-1"
access_key = var.access_key
secret_key = var.secret_key
}
resource "aws_dynamodb_table" "state_locking" {
hash_key = "LockID"
name = "dynamodb-state-locking"
attribute {
name = "LockID"
type = "S"
}
billing_mode = "PAY_PER_REQUEST"
}
resource "aws_instance" "ec2_example" {
ami = "ami-0767046d1677be5a0"
instance_type = "t2.micro"
tags = {
Name = "EC2 Instance with remote state"
}
}
terraform {
backend "s3" {
bucket = "terraform-s3-bucket"
key = "terraform/remote/s3/terraform.tfstate"
region = "us-east-1"
}
}
terraform {
backend "s3" {
bucket = "terraform-s3-bucket"
key = "terraform/remote/s3/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "dynamodb-state-locking"
}
}
provider "aws" {
region = "us-east-1"
access_key = var.access_key
secret_key = var.secret_key
}
resource "aws_dynamodb_table" "state_locking" {
hash_key = "LockID"
name = "dynamodb-state-locking"
attribute {
name = "LockID"
type = "S"
}
billing_mode = "PAY_PER_REQUEST"
}
resource "aws_instance" "ec2_example" {
ami = "ami-0767046d1677be5a0"
instance_type = "t2.micro"
tags = {
Name = "EC2 Instance with remote state"
}
}
terraform {
backend "s3" {
bucket = "terraform-s3-bucket"
key = "terraform/remote/s3/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "dynamodb-state-locking"
}
}

3.1 Apply the above terraform configuration with the DynamoDB table

4. Spin one more EC2 instance with the same Terraform state file

provider "aws" {
region = "us-east-1"
access_key = var.access_key
secret_key = var.secret_key
}
resource "aws_instance" "ec2_example" {
ami = "ami-0767046d1677be5a0"
instance_type = "t2.micro"
tags = {
Name = "EC2 Instance with remote state"
}
}
terraform {
backend "s3" {
bucket = "terraform-s3-bucket"
key = "terraform/remote/s3/terraform.tfstate"
encrypt = true
region = "us-east-1"
dynamodb_table = "dynamodb-state-locking"
}
}

4.1 Run both the terraform files at the same time to simulate the Locking on the Terraform state file

5. Conclusion

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to enable case insensitive search in AEM with Lucene?

How To Upload Items with FBDI

Basics of Kotlin- Part 2

Elixir Awesome Project 2021

This Week In TurtleCoin (July 23, 2019)

Design Approach to get Spring Service Implementation at Runtime

Windows 11 how to unstack taskbar

Closures in Python

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
ABHISHEK KUMAR

ABHISHEK KUMAR

More from Medium

Scan Terraform with OPA

[Terraform]: Build Amazon Redis Cache from SNAPShots not are supported. Wait What!!

Exclude ‘Objects have changed outside of Terraform’ from GitHub Actions output

Getting started with Terraform