Remote Backends and State File Locking in Terraform

Remote Backends and State File Locking in Terraform

Overview:-

In this blog, we will Implement Remote Backends and State File Locking in our Terraform Project.

What is Remote Backends?

Terraform stores the state file on our local machine. It is fine if we are working solo on the projects but if a team or multiple people is working on a single project then it can create problems to handle the state file. Here Remote Backends come into use remote backends are shareable storage for our Infrastructure. It is on Cloud Platforms like AWS. Suppose our State FIle is in an S3 bucket instead of our Local Machine.

In simple words, remote backends help us to store our infrastructure sate in a place where multiple people can access it.

What is State File Locking?

When a team or multiple people work on the same infrastructure we need a way that there is no collapse between people. It means that we don't want that at a single time multiple people should make changes in the Infrastructure so we need a thing that will prevent people from making changes at one time. Here State File Locking comes into action as its name says a person gets locked to make changes while the other waits. This Prevents conflicts and properly keeps everything.

In simple words, State file locking is a way that only one person can change the infrastructure at a time to avoid conflicts in the team.

Creating a Server, Setting up Terraform on the Server, Linking our AWS Account with the Server:-

  • At first, we need to create an EC2 Server, Set Terraform on it and Link our AWS Account with our server. This will be done to create our project by Implementing Remote Backend and State File Locking.

  • To Create a Server, set Terraform on the Server and Link our AWS account with the Server follow my blog-

    Blog

    Follow the Steps which are in the Red area not all the Steps.

Creating an Infrastructure:-

  • We created a server, did the setup of Terraform and Linked our AWS Account.

  • Now we will create an infrastructure on which we will Implement Remote Backends and State File Locking.

  • First, create a directory and navigate to it by the commands:

      mkdir inner_infra
      cdc inner_infra
    

  • Create a file by the Command:

      vim terraform.tf
    

    In this file Paste these Code:

      terraform {
        required_providers {
          aws = {
            source  = "hashicorp/aws"
            version = "4.66.1"
          }
        }
      }
    

  • Now create another file by the command:

      vim providers.tf
    

Paste these Code in this file:

provider "aws" {
  region = "us-east-2"
}

My region is us-east-2 but your can be different so check it.

  • Create one more file by the Command:

      vim resources.tf
    

In this file Paste these Code:

resource "aws_s3_bucket" "bucke" {
  bucket = "buckkkktest"
}

resource "aws_dynamodb_table" "my_dynamo_table" {
  name         = "db_table_for_test"
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = "LockID"
  attribute {
    name = "LockID"
    type = "S"
  }
}

You can Replace the bucket name and dynamoDB Table name if any error comes that the name is already used. We have Created Infrastructure. It has a S3 bucket and a DynamoDB Table.

  • To apply these codes so that the S3 bucket and Table can be created on our Account run these Commands one by one:

      terraform init
    

      terraform plan
    

      terraform apply
    

  • When we run the command ls we will see that the state files of our project are present here:

      ls
    

  • When we Open our Account we will see that we have a S3 bucket and DynamoDB Table Created.

Implementing Remote Backends and State file Locking in the Infrastructure:-

  • We have created a Simple Infraestiure which has a S3 bucket and a DynamoDB Table. Now we will Implement Remote Backends and State file Locking in our Infrastructure.

  • First, Navigate to the root directory, create a new directory and navigate to the directory created. Run these Commands one by one:-

      cd ..
      mkdir outer_infra
      cd outer_infra
    

  • Now create a file by the Command:

      vim terraform.tf
    

In this file paste these codes:

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "4.66.1"
    }
  }

  backend "s3" {
    bucket         = "buckkkktest"
    key            = "terraform.tfstate"
    region         = "us-east-2"
    dynamodb_table = "db_table_for_test"
  }
}

Make sure to replace your bucket name and dynamo table name if you have used different names. Match your region also.

  • Create one more file by the Command:

      vim main.tf
    

In this file paste these codes:

provider "aws" {
  region = "us-east-2"
}

resource "aws_instance" "my_ec2_instance" {
  ami           = "ami-024e6efaf93d85776"
  instance_type = "t2.micro"
  tags = {
    Name = "terra-prac"
  }
}

Again verify your region and also ami varies from region to region.

  • We have created a Remote Backend and State file Locking, now we will just plan and apply it. Run these Commands one by one.

      terraform plan
      terraform apply
    
  • After Planning and Applying it when we will do ls we will see that we don't have State files on our Local Machine. Run the Command:

      ls
    

So where is our State file?

  • When we will open our AWS Account and Navigate to the bucket Created we will see that our state file is present there.

Now here a team or multiple persons can access it as it's not on anyone's local machine.

  • For State file locking when we navigate to the DynamoDB Table and click on Explore Table items from the top right side.

  • Here we will see that at the bottom there is a LockID this ID will prevent Multiple people from making changes in the infrastructure and a single person can make changes in the infrastructure at one time.

Whenever anyone makes changes in the infrastructure a LockID will be generated that will prevent multiple changes at a single time.

All the Source Code are here also:

GitHub

Thanks.