Terraform for DevOps: Managing State & Remote Backends (Part 3)

Mar 30, 2025
7 min read
1294 words

Hey DevOps friends! 🚀 Welcome back to our Terraform tutorial series. In our previous posts, we explored the power of Infrastructure as Code and got hands-on with setting up and deploying Terraform. Now, in Part 3, we’re diving deep into Terraform Backends & State Management. We'll explore everything from local backends to remote setups, and learn how to securely manage and collaborate on your infrastructure's state. Ready to unlock the secrets behind robust state management? Let’s dive in!

💬 Got Questions? If you have any questions or need further clarification while reading this post, please don't hesitate to drop a comment below! I'm here to help, and I'll gladly create new posts to dive deeper into any topics you find challenging. 😊

1. Backends

Backends in Terraform dictate where your state file—the beating heart of your deployed infrastructure—lives. Picking the right backend isn’t just a tech detail; it’s a power move for security, team sync, and automation. Here’s the rundown of the two main flavors::

1.1. Local Backend

The local backend is Terraform’s default setup: it parks your state file—Terraform’s record of your live infrastructure—right alongside your configuration files on your local machine. Picture it like keeping your blueprints and project notes in the same desk drawer—everything’s at your fingertips, no external dependencies required.

Pros

  • Simplicity: Easy to set up and use, making it a great choice for beginners starting with Terraform.
  • Quick Start: Requires no extra steps—just write your code and run Terraform locally to begin development.

Cons

  • Security Risks: The state file stores sensitive information (like keys) in plain text, which can be a problem if not protected.
  • Limited Collaboration: Works best for one person; sharing the state file with a team is difficult and inefficient.
  • Manual Management: Terraform commands rely on the local state file, which can complicate automated processes or large workflow

1.2. Remote Backend

A remote backend takes your Terraform state file—your infrastructure’s live record—and stores it on a remote server instead of your local machine. By decoupling the state from your local environment, it opens the door to better security, team collaboration, and automation. You can set this up using services like Terraform Cloud or cloud provider storage options, such as AWS S3 paired with DynamoDB for state locking.

Pros

  • Enhanced Security: State files can be encrypted and stored with access controls (e.g., S3 bucket policies or Terraform Cloud permissions), keeping sensitive data like credentials safe from prying eyes.
  • Collaboration: Teams can share a single state file hosted remotely, making it easy for multiple people to work together without passing files around or risking conflicts.
  • Automation: Integrates smoothly with CI/CD pipelines (e.g., GitHub Actions, Jenkins), enabling hands-off infrastructure management and remote operations from anywhere.

Cons

  • Increased Complexity: Setting up a remote backend—like configuring Terraform Cloud or an S3 bucket with DynamoDB—takes more steps and planning than the plug-and-play local option.
  • Dependency on External Services: Relies on third-party systems (e.g., AWS, Terraform Cloud), which can introduce costs, maintenance, or downtime risks if those services falter.

Choosing the right backend for your project is crucial to ensuring security, collaboration, and seamless integration into your workflow. For small, personal projects, a local backend may be sufficient. However, in team environments and production systems, a remote backend is highly recommended, as it enhances security, enables collaboration, and supports automated workflows.

2. Remote Backend

Terraform supports remote backends for storing state files in a centralized and secure location, enabling collaboration, security, and automation. The two primary options for managing remote state are Terraform Cloud, a managed solution offering built-in state management and team collaboration, and AWS S3 with DynamoDB, a self-managed approach that provides flexibility, encryption, and state locking for concurrent operations.

2.1. Option 1: Terraform Cloud Backend

Terraform Cloud, a fully managed service by HashiCorp, simplifies state management by providing a secure, scalable, and collaborative backend solution. It eliminates the need for manual state file handling, offering features like remote execution, versioning, and team-based access controls.

✅ Key Features of Terraform Cloud Backend

✔️ Secure State Management – Stores and encrypts Terraform state files, ensuring data integrity.
✔️ Team Collaboration – Allows multiple users to work on the same infrastructure (Free for up to 5 users).
✔️ Zero Infrastructure Overhead – No need to set up and manage backend storage manually.
✔️ Seamless Integration – Works with Terraform workspaces and organizations for better project organization.

🔧 Steps to Set Up Terraform Cloud

  • Create a Terraform Cloud Account.
  • Create an Organization.
  • Create a Workspace.
  • Authenticate Terraform CLI : terraform login

🛠️ Usage Example To use Terraform Cloud as a backend, define the following in your terraform block:

terraform {
  backend "remote" {
    hostname     = "app.terraform.io"
    organization = "your-org"
 
    workspaces {
      name = "your-workspace"
    }
  }
}

Your Terraform Cloud backend is now configured! 🎉

2.2. Option 2: AWS S3 + DynamoDB Backend

For teams that prefer a self-managed backend, AWS S3 (for state storage) and DynamoDB (for state locking) provide a secure, scalable, and cost-effective solution.

  • S3 Bucket → Stores the Terraform state file securely with versioning and encryption.
  • DynamoDB Table → Enables state locking, preventing multiple users from making conflicting changes during deployments.

⚠️ Challenge: The Chicken & Egg Problem How do you provision an S3 backend with Terraform if there's no infrastructure to store the state file yet? 🤔

✅ Solution: Bootstrap Process!

2.2.1 Step 1: Bootstrap Remote Backend Resources

Before using an AWS backend, we must manually create:

1️⃣ An S3 bucket for storing the state file. 2️⃣ A DynamoDB table for managing Terraform state locks.

resource "aws_s3_bucket" "terraform_state" {
  bucket = "my-terraform-state-bucket"
  versioning {
    enabled = true
  }
  server_side_encryption_configuration {
    rule {
      apply_server_side_encryption_by_default {
        sse_algorithm = "AES256"
      }
    }
  }
}
 
resource "aws_dynamodb_table" "terraform_locks" {
  name           = "terraform-locks"
  billing_mode   = "PAY_PER_REQUEST"
  hash_key       = "LockID"
 
  attribute {
    name = "LockID"
    type = "S"
  }
}

Run:

terraform init
terraform apply

2.2.2. Step 2: Configure Terraform to Use AWS Remote Backend

Once the S3 bucket and DynamoDB table exist, update backend settings:

terraform {
  backend "s3" {
    bucket         = "my-terraform-state-bucket"
    key            = "terraform.tfstate"
    region         = "us-east-1"
    encrypt        = true
    dynamodb_table = "terraform-locks"
  }
}

Then run:

terraform init
terraform plan
terraform apply

🎯 Which Remote Backend Should You Use? Feature Terraform Cloud AWS S3 + DynamoDB Ease of Setup ✅ Easy (No Infra Needed) ❌ Requires Manual Setup State Locking ✅ Built-in ✅ DynamoDB Security ✅ Encrypted by Default ✅ Configurable in S3 Collaboration ✅ Teams & Workspaces ✅ IAM Policies Cost ⚡ Free (Up to 5 users) ⚡ AWS Storage & Read/Write Use Case 🚀 Managed, Best for Teams 🔧 Self-Managed, Best for Enterprises

3. Final Thoughts

Both solutions are effective, but choosing the right one depends on your needs:

✅ Terraform Cloud is best for quick setups, built-in security, and seamless team collaboration. ✅ AWS S3 + DynamoDB offers full control, customization, and is ideal for organizations with strict infrastructure policies.

Pick the solution that aligns with your workflow and security requirements! 🚀

Demo & Code Repository

You can find the complete Terraform configurations for this tutorial in the GitHub repository below. Feel free to explore, fork, and experiment! 🚀

🔗 GitHub Repo: [https://github.com/rahimbtc1994/terraform-intermediate/tree/main/part-3]

Now that you've mastered Terraform state management and backends, it's time to make your configurations more flexible! In Terraform for DevOps: Variables, Outputs, and Dynamic Workflows (Part 4), we'll explore how to use variables, outputs, and advanced Terraform features to create reusable and dynamic infrastructure. Keep building your Terraform expertise—let's go! 🚀

If you have any questions or run into issues, drop a comment—I’m happy to help! 😊

Rahim's Newsletter

I write monthly Tech, Web Development and chrome extension that will improve your productivity. Trust me, I won't spam you.

Share on Social Media: