Tuesday, 9 September 2025

Mastering Terraform Modules: Reusable Infrastructure Code Made Simple (part 6)

Standard

When building infrastructure with Terraform, copying and pasting the same code across projects quickly becomes messy.

Terraform Modules solve this by letting you write code once and reuse it anywhere—for dev, staging, production, or even multiple teams.

In this blog, you’ll learn:

  • What Terraform Modules are
  • How to create and use them
  • Real-world examples and best practices

What Are Terraform Modules?

A module in Terraform is just a folder with Terraform configuration files (.tf) that define resources.

  • Root module → Your main project directory.
  • Child module → A reusable block of Terraform code you call from the root module.

Think of modules as functions in programming:

  • Input → Variables
  • Logic → Resources
  • Output → Resource details

Why Use Modules?

  1. Reusability Write once, use anywhere.
  2. MaintainabilityFix bugs in one place, apply everywhere.
  3. Consistency Ensure similar setups across environments.
  4. CollaborationShare modules across teams.

Creating Your First Terraform Module

Step 1: Create Module Folder

terraform-project/
  main.tf
  variables.tf
  outputs.tf
  modules/
    s3_bucket/
      main.tf
      variables.tf
      outputs.tf

Step 2: Define the Module (modules/s3_bucket/main.tf)

variable "bucket_name" {
  description = "Name of the S3 bucket"
  type        = string
}

resource "aws_s3_bucket" "this" {
  bucket = var.bucket_name
  acl    = "private"
}

output "bucket_arn" {
  value = aws_s3_bucket.this.arn
}

Step 3: Call the Module in main.tf

module "my_s3_bucket" {
  source      = "./modules/s3_bucket"
  bucket_name = "my-production-bucket"
}

Run:

terraform init
terraform apply

Terraform will create the S3 bucket using the module.

Using Modules from Terraform Registry

You can also use prebuilt modules:

module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "3.14.0"

  name = "my-vpc"
  cidr = "10.0.0.0/16"
}

The Terraform Registry has official modules for AWS, Azure, GCP, and more.

Case Study: Multi-Environment Infrastructure

A startup had:

  • Dev environment → Small resources
  • Staging environment → Medium resources
  • Production environment → High availability setup

They created one module for VPC, EC2, and S3:

  • Passed environment-specific variables (instance size, tags).
  • Reused the same modules for all environments.

Result: Reduced code duplication by 80%, simplified maintenance.

Best Practices for Modules

  1. Keep modules smallEach should focus on one task (e.g., S3, VPC).
  2. Version your modulesTag releases in Git for stability.
  3. Use meaningful variables & outputs for clarity.
  4. Avoid hardcoding values always use variables.
  5. Document your modules so teams can reuse them easily.

Project Structure with Modules

terraform-project/
  main.tf
  variables.tf
  outputs.tf
  terraform.tfvars
  modules/
    s3_bucket/
      main.tf
      variables.tf
      outputs.tf
    vpc/
      main.tf
      variables.tf
      outputs.tf

What’s Next?

Now you know how to:

  • Create your own modules

  • Reuse community modules

  • Build cleaner, scalable infrastructure

In Part 7, we’ll explore Workspaces & Environments to manage dev, staging, and prod in one Terraform project.

Bibliography