Skip to main content

Command Palette

Search for a command to run...

Day 80 of 100 Days : Terraform State Files and Remote Backends

Published
•3 min read
M

"Aspiring DevOps Engineer on a 100-day journey to master the principles, tools, and practices of DevOps. Sharing daily insights, practical lessons, and hands-on projects to document my path from beginner to proficient. Passionate about continuous learning, automation, and bridging the gap between development and operations. Join me as I explore the world of DevOps, one day at a time!"

Hey DevOps Enthusiasts! šŸŽ‰
Today, we’re diving into Terraform State Files—the heart and soul of Terraform’s ability to manage your infrastructure efficiently. Let’s explore why we need it, what happens without it, and how to secure it using remote backends like S3. We’ll end with a hands-on demo to tie everything together. Ready? Let’s go! šŸš€


What is a Terraform State File? šŸ“

The Terraform state file (terraform.tfstate) is Terraform’s way of remembering the current state of your infrastructure. It’s like your personal journal for Terraform—keeping track of what resources exist, their configuration, and relationships.

Why Do We Need a State File?

Imagine Terraform as a builder. Without a blueprint, how would it know what’s already built and what’s pending? That’s what the state file is—a blueprint for your infrastructure. It ensures:

  1. Resource tracking: Avoid creating duplicate resources.

  2. Change management: Identify updates and deletions during terraform plan.

  3. Team collaboration: Share a single source of truth with others.


What Happens Without a State File? šŸ› ļø

Without a state file, Terraform would recreate everything from scratch every time you run it. 😱 Imagine destroying a perfectly good EC2 instance just to create a new one! That’s messy and risky.

Here’s a quick analogy:
Building a sandcastle at the beach. Without keeping track of your castle’s current state, you might knock it down by accident while trying to improve it!


Remote Backends: The Saviors of State Management šŸ›”ļø

By default, Terraform stores the state file locally, but this has drawbacks:

  • Risk of accidental deletion 😟

  • Challenges with team collaboration šŸ§‘ā€šŸ¤ā€šŸ§‘

  • Security concerns if sensitive data is in the state file šŸ”

Enter Remote Backends! šŸŽ‰ Remote backends like S3, Terraform Cloud, and others:

  • Store the state file securely.

  • Allow collaboration.

  • Enable locking to prevent conflicts.

In this blog, we’ll set up an S3 backend for state file storage.


Let’s Get Hands-On! A Cheerful Demo Awaits šŸ˜„

Step 1: Setting Up main.tf

Here’s the main.tf to deploy an EC2 instance.

# Specify the AWS provider
provider "aws" {
  region = "ap-south-1"
}

# Resource: EC2 Instance
resource "aws_instance" "muni" {
  ami           = "ami-053b12d3152c0cc71" # Replace with a valid AMI ID for your region
  instance_type = "t2.micro"
  key_name      = "aws_logins"            # Replace with your actual key pair name

  # Optional tags to organize your resources
  tags = {
    Name = "MuniInstance"
  }
}

Step 2: Configuring backend.tf for Remote Backend

This backend.tf will store the state file in an S3 bucket.

terraform {
  backend "s3" {
    bucket         = "muni-demo-s3-xyz"      # Name of the S3 bucket
    region         = "ap-south-1"            # AWS region of the bucket
    key            = "muni/terraform.tfstate" # Path for the state file in the bucket

  }
}

Step 3: Running the Commands

  1. Initialize Terraform
    Run:

     terraform init
    

    šŸŽ‰ Terraform will now configure the backend. You’ll see a message indicating that the state file is moved to S3.

  2. Preview the Plan
    Run:

     terraform plan
    

    See the list of resources to be created. Doesn’t it feel good to know what’s happening? 😊

  3. Apply the Changes
    Run:

     terraform apply
    

    Type yes to deploy the resources.


Why This Matters: Scenarios That Prove Its Worth šŸ’”

  1. Team Collaboration: Sharing the state file ensures that everyone works on the same infrastructure version.

  2. Disaster Recovery: Accidentally deleted the local state file? Don’t worry—your S3 backup has you covered!

  3. Security: Sensitive data in the state file is now securely stored and encrypted.


Closing Thoughts on Day 80 🌟

Congratulations on learning about Terraform state files and remote backends! šŸŽ‰ You’ve just taken another step towards mastering Terraform and ensuring a secure and collaborative workflow.

With a happy heart and a growing skill set, let’s keep building amazing things. šŸš€
See you on Day 81!

More from this blog

1

100-Day DevOps Journey Blog

100 posts