Terraform - Backend (AWS)

Ensuring Consistency and Collaboration with Remote State Management in Terraform.

·

4 min read

Terraform maintains the state of the infrastructure locally on the user's machine. However, this approach becomes problematic in a team environment where different team members may end up with different states, leading to inconsistencies.

To solve this, Terraform state files need to be synchronized across all team members (same in all environments). The best and easiest way to achieve this is by storing the Terraform state file in a remote location, such as an S3 bucket.

The objective is to store the Terraform state file in a centralized location. This ensures that all team members have access to the same state file, preventing inconsistencies and enabling better collaboration. By using a remote backend like an S3 bucket, the state file is synchronized across all environments and team members.

provider.tf This is an example file of defining an AWS provider in Terraform, setting the region based on the REGION variable.

provider "aws" {
  region = var.REGION
}

var.tf This is an example file of defining Terraform variables. It includes three variables:

  • REGION: A variable with a default value of "us-east-1".

  • ZONE1: A variable with a default value of "us-east-2a".

  • AMIS: A map variable with different AMI IDs for different regions, with default values for "us-east-2" and "us-east-1".

variable "REGION" {
  default = "us-east-1"
}
variable "ZONE1" {
  default = "us-east-2a"
}
variable "AMIS" {
  type = map(any)
  default = {
    us-east-2 = "ami-03657b56516ab7912"
    us-east-1 = "ami-0b72821e2f351e396"
  }
}

The backend.tf file you provided configures Terraform to use an S3 bucket as the backend for storing the state file. Here's a breakdown of the configuration:

terraform {
  backend "s3" {
    bucket = "terraform-bucket-ragavi"
    key    = "terraform/backend"
    region = "us-east-2"
  }
}
  • terraform: This block is used to configure settings related to Terraform itself, such as the backend configuration.

  • backend "s3": This specifies that the backend type is S3. Terraform will use an Amazon S3 bucket to store the state file.

  • bucket = "terraform-bucket-ragavi": This is the name of the S3 bucket where the Terraform state file will be stored. In this case, the bucket name is terraform-bucket-ragavi.

  • key = "terraform/backend": This specifies the path within the S3 bucket where the state file will be stored. Here, the state file will be stored at terraform/backend.

  • region = "us-east-2": This specifies the AWS region where the S3 bucket is located. In this case, the region is us-east-2.

By configuring the backend in this way, Terraform ensures that the state file is stored remotely in the specified S3 bucket, allowing for better collaboration and state management in a team environment.

Then apply these commands,

  1. terraform fmt This command formats the Terraform configuration files in the current directory to a canonical format and style. It ensures that the code is properly indented and organized, making it easier to read and maintain.
terraform fmt
  1. terraform init This command initializes a Terraform working directory. It prepares the directory by downloading and installing the necessary provider plugins and setting up the backend configuration. This is the first command that should be run after writing a new Terraform configuration or cloning an existing one from version control.
terraform init
  1. terraform validate This command checks the syntax and validity of the Terraform configuration files in the current directory. It ensures that the configuration is syntactically correct and that all required arguments are specified. This is a useful step to catch errors before applying the configuration.
terraform validate
  1. terraform plan This command creates an execution plan, showing what actions Terraform will take to achieve the desired state defined in the configuration files. It compares the current state with the desired state and lists the changes that will be made such as creating, updating, or deleting resources. This allows you to review the proposed changes before applying them.
terraform plan
  1. terraform apply This command applies the changes required to reach the desired state of the configuration as defined by the Terraform files. It creates, updates or deletes infrastructure resources to match the configuration. Before making any changes, Terraform will show a plan of the actions it will take and ask for your confirmation.
terraform apply
  1. terraform destroy This command is used to destroy the infrastructure managed by Terraform. It will remove all the resources defined in your Terraform configuration files. Before making any changes, Terraform will show a plan of the actions it will take and ask for your confirmation. This is useful for tearing down environments or cleaning up resources that are no longer needed.
terraform destroy

Refer:

https://github.com/Ragavi04P/Terraform-Practice/tree/main/exercise5