Understanding Terraform: A Friendly Guide to State Management, Locking, Remote Backend, and Modules

kalyan dahakekalyan dahake
5 min read

Terraform is a popular Infrastructure-as-Code (IaC) tool that allows you to manage and provision infrastructure in a repeatable, efficient, and consistent way. Whether you're provisioning a server on AWS, creating a network on Azure, or managing resources on GCP, Terraform lets you describe your infrastructure using high-level configuration files.

In this blog, we'll explore key concepts such as State Management, Locking, Remote Backends, and Modules in Terraform. We'll also go through some examples to help you get started.


1. State Management in Terraform

State management is one of the key features of Terraform. It allows Terraform to keep track of the resources it has created or modified. Terraform uses a state file to map the configuration to the real-world infrastructure.

Why is State Important?

When you make changes to your infrastructure, Terraform compares your current configuration with the state file to determine which resources need to be created, updated, or deleted. This helps Terraform understand the "current state" of your infrastructure and make the necessary adjustments.

Terraform State Example

By default, Terraform stores its state locally in a file called terraform.tfstate. Here’s an example:

terraform {
  backend "local" {
    path = "./terraform.tfstate"
  }
}

resource "aws_s3_bucket" "my_bucket" {
  bucket = "my-tf-state-bucket"
  acl    = "private"
}

In this example, Terraform creates an S3 bucket and stores its state in a file called terraform.tfstate. This file is essential because it keeps track of the bucket's properties and helps Terraform determine whether the resource needs to be updated.

State Management Best Practices

  • Avoid Direct State Manipulation: Always use Terraform commands like terraform plan, terraform apply, and terraform destroy to manage the state.

  • Backup State Files: Store your state files in a secure, backed-up location to avoid losing valuable infrastructure data.

  • State Locking: When working in teams, you’ll want to avoid race conditions where multiple users try to modify the state simultaneously. This leads us to the next topic—Locking.


2. Locking in Terraform

Locking is an essential feature when working with Terraform in a team. It ensures that multiple people or processes can't modify the same infrastructure at the same time, which could lead to conflicts or inconsistencies.

What is State Locking?

Terraform supports state locking, which prevents other users or processes from modifying the state while it's being updated. When Terraform starts an operation (like apply), it automatically locks the state to avoid concurrent modifications.

In remote backends like S3, Terraform uses services like DynamoDB (for AWS) to enable locking.

Example of Locking with AWS S3 and DynamoDB

Here’s an example of setting up state locking with an S3 backend and DynamoDB for Terraform:

terraform {
  backend "s3" {
    bucket         = "my-tf-state-bucket"
    key            = "state/terraform.tfstate"
    region         = "ap-south-1"
    dynamodb_table = "my-tf-lock-table"
    encrypt        = true
  }
}

In this setup:

  • S3 is used to store the state file.

  • DynamoDB is used to enable state locking.

Terraform will now lock the state in the DynamoDB table while running operations, ensuring that no other processes or users can modify it at the same time.


3. Remote Backend in Terraform

A backend in Terraform defines where and how Terraform stores its state. By default, Terraform uses a local backend (storing state in the local file system). However, for teams or large-scale environments, it’s recommended to use remote backends.

Benefits of Using Remote Backends:

  • Collaboration: Remote backends allow multiple users to collaborate by sharing the same state file.

  • Consistency: State is centrally stored, reducing the risk of conflicts between local and remote state versions.

  • Backup & Security: Remote backends typically come with built-in security and backup mechanisms.

Example of Remote Backend with S3

You can configure a remote backend with an S3 bucket and DynamoDB table for state locking:

terraform {
  backend "s3" {
    bucket         = "my-tf-state-bucket"
    key            = "terraform/state"
    region         = "ap-south-1"
    encrypt        = true
    dynamodb_table = "my-tf-lock-table"
  }
}

How to Initialize the Backend

Once you’ve configured your backend, you need to run the following command to initialize Terraform with your remote backend configuration:

terraform init

Terraform will now use the remote backend to store and manage your state file.


4. Modules in Terraform

Modules in Terraform are reusable, self-contained units of configuration. They are great for organizing your infrastructure and making your Terraform configurations more modular, readable, and maintainable.

Why Use Modules?

  • Reusability: You can reuse modules across multiple projects or environments.

  • Organization: Helps organize complex configurations into smaller, more manageable pieces.

  • Maintainability: Modifications to modules are easier and can be applied across multiple environments at once.

Example of a Module

Let’s create a simple module for creating an AWS EC2 instance.

modules/ec2-instance/main.tf:

resource "aws_instance" "example" {
  ami           = var.ami
  instance_type = var.instance_type
}

variable "ami" {}
variable "instance_type" {}

main.tf:

module "ec2_instance" {
  source        = "./modules/ec2-instance"
  ami           = "ami-12345678"
  instance_type = "t2.micro"
}

In this example, we’ve created a module to provision an EC2 instance. In the root configuration (main.tf), we refer to this module and pass the necessary variables (ami and instance_type).

Running the Module

To use this module, run the following Terraform commands:

terraform init
terraform apply

Terraform will now create the EC2 instance as defined in the module.


Conclusion

Terraform is a powerful tool for managing infrastructure, and understanding its core concepts like State Management, Locking, Remote Backend, and Modules will help you use it more effectively. These features enable collaboration, consistency, and maintainability of infrastructure as code, making Terraform an essential tool for teams and large-scale environments.

By implementing remote backends, locking mechanisms, and modules, you can ensure that your Terraform projects are secure, organized, and easy to manage. Happy coding with Terraform!

3
Subscribe to my newsletter

Read articles from kalyan dahake directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

kalyan dahake
kalyan dahake

Passionate about Backend Engineering, Love to design systems and services and to explore tech