Terraform Local State and Remote State Management

Terraform Local State and Remote State Management

ยท

6 min read

TerraWeek Day 4/7

Terraform is a popular Infrastructure as Code (IaC) tool that is used to provision and manage cloud resources across different cloud providers such as AWS and Azure. One of the key aspects of Terraform is its state management feature, which helps track the current state of resources and ensures smooth infrastructure provisioning and management.

In this blog post, we will dive into the importance of Terraform state, different methods of storing state files, and how to leverage remote state management.

Importance of Terraform State

Terraform state is a critical aspect of managing infrastructure using Terraform. Terraform's state file is a JSON-formatted file that contains all the information about the created resources, including variables, configurations, dependencies, and relationships between resources.

It's a record of the state of the infrastructure that Terraform manages, and it's essential for resource management and tracking.

The Terraform state file is used to determine what infrastructure resources to create, delete, or modify. It also keeps track of configuration changes, such as updates to resource properties or dependencies. The Terraform state file is critical in managing infrastructure as it:

  • Tracks change in infrastructure over time

  • Ensures the actual infrastructure state matches the desired state

  • Helps maintain consistency by preventing changes that conflict with existing resources

Local State and terraform state Command

By default, Terraform stores the state file locally on the machine running the Terraform command. This method is called local state storage and is suitable for small and simple infrastructures. Managing state files manually can lead to errors and data loss, which is why Terraform provides the terraform state command to manage the state file, including show, list, and remove commands.

Local State Storage

To enable local state storage, you don't need to do anything special. This is the default behavior of Terraform. When you run a command like terraform apply, Terraform will automatically create a state file named terraform.tfstate in the same directory as your configuration file.

Managing State with "terraform state" command

You can use the terraform state command to view, modify, and delete resources within your state file. Here are some examples:

  • To list all the resources in your state file, run:

terraform state list

  • To view the current state of a specific resource, run:

terraform state show [resource-name]

Replace 'resource-name' with the name of the resource you want to show the current state of.

  • To remove a resource from the state file, run:

terraform state rm [resource-name]

This will remove the specified resource and its associated state from the state file. Use this command with caution - removing a resource from the state file can cause issues during future operations.

Remote State Management

With remote state management, you store the state file remotely, allowing multiple team members to work together on the same infrastructure code without affecting each other. There are various options for remote state management, including Terraform Cloud, AWS S3, Azure Storage Account, or HashiCorp Consul.

When using remote state management, the Terraform state is stored in a central location that can be accessed by all team members involved in the project. This helps eliminate issues associated with local state management, such as conflicting changes, version control issues, and the risk of losing the state file.

To store the state remotely in an S3 bucket using the backend configuration block:

# backend.tf
terraform {
  backend "s3" {
    bucket = "my-terraform-state-bucket"
    key    = "terraform.tfstate"
    region = "us-west-2"
  }
}

And now, here is the complete step-by-step guide to using remote state management with Terraform and AWS S3.

Step 1: Create an S3 bucket

The first step to using S3 as the remote state backend is to create an S3 bucket in your AWS account. You can create the bucket through the AWS Management Console or use the resource in your Terraform configuration file. Keep in mind that the bucket name must be globally unique across all of AWS, so choose a unique name. You should also consider enabling versioning on the bucket to avoid losing any previous versions of your Terraform state.

Step 2: Modify your Terraform configuration file

Once you have an S3 bucket, you can modify your Terraform configuration file to use S3 as the remote state backend. You'll need to add the backend configuration block to your configuration file. Here's an example:

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.16"
    }
  }
  required_version = "~> 1.3.9"
}

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

resource "local_file" "DevOps" {
        filename = "C:/Users/Chait/Desktop/Terrform/remote_state_management/terra_generated.txt"
        content = "I am a DevOps engineer, who knows terraform very well"
}

resource "aws_s3_bucket" "my_bucket" {
  bucket = "chaitannyaa-terraform-state-bucket"
  lifecycle {
    prevent_destroy = true
  }
}

resource "aws_s3_bucket_versioning" "my_bucket_versioning" {
  bucket = aws_s3_bucket.my_bucket.id

  versioning_configuration {
    status = "Enabled"
  }

terraform {
  backend "<chosen_backend>" {
    # Add required configuration options for the chosen backend
  }
}
}

This configuration tells Terraform to use the S3 bucket my-terraform-state-bucket to store the state file. The key parameter specifies the filename for the state file within the bucket. Finally, the region parameter specifies the AWS region where the bucket is located.

Step 3: Initialize the backend

After modifying your Terraform configuration file, you can initialize the S3 backend by running the terraform init command. This will download the necessary provider plugins and create the remote state bucket in S3.

terraform init

image

Step 4: Use your configuration file to manage resources

Now that you have set up the remote state backend, you can use your Terraform configuration file to manage resources. When you run terraform apply, Terraform will automatically store the state file in the S3 bucket you specified.

terraform apply

image

image

image

image

Step 5: Access your state file

If you need to access your state file for any reason, you can use the terraform state command. For example, to list all the resources in your state file, run:

terraform state list

image

Remote State Configuration

๐Ÿ“š Modify your Terraform configuration file to store the state remotely using the chosen remote state management option. Include the necessary backend configuration block in your Terraform configuration file to enable seamless remote state storage and access.

terraform {
  backend "<chosen_backend>" {
    # Add required configuration options for the chosen backend
  }
}

Let's add a backend S3 block to use it for remote state files storage--->

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.16"
    }
  }
  required_version = "~> 1.3.9"
}

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

resource "local_file" "DevOps" {
        filename = "C:/Users/Chait/Desktop/Terrform/remote_state_management/terra_generated.txt"
        content = "I am a DevOps engineer, who knows terraform very well"
}

resource "aws_s3_bucket" "my_bucket" {
  bucket = "chaitannyaa-terraform-state-bucket"
  lifecycle {
    prevent_destroy = true
  }
}

resource "aws_s3_bucket_versioning" "my_bucket_versioning" {
  bucket = aws_s3_bucket.my_bucket.id

  versioning_configuration {
    status = "Enabled"
  }
}

terraform {
  backend "s3" {
    bucket = "chaitannyaa-terraform-state-bucket"
    key    = "terraform.tfstate"
    region = "us-east-1"
  }
}

terraform init

image

image

terraform apply

image

image

image

Now do change your terraform configuration to operate changes to it--->

terraform apply

image

image

image

image

I hope you got the use of remote state storage management using AWS S3.

Happy Learning :)

#TrainWithShubham #TerraWeek Challenge

I hope you learned something today with me!

Stay tuned for my next blog on "Day5 of Terraweek Challenge". I will keep sharing my learnings and knowledge here with you.

Let's learn together! I appreciate any comments or suggestions you may have to improve my learning and blog content.

Thank you,

Chaitannyaa Gaikwad

(19) Chaitannyaa Gaikwad | LinkedIn

Did you find this article valuable?

Support Chaitannyaa Gaikwad by becoming a sponsor. Any amount is appreciated!

ย