Skip to main content

Terraform Modules

Imagine you’re building a Lego city with Terraform. At first, you manually place each house, road, and park. But soon, you realize you’re repeating the same patterns - every neighborhood needs houses, every block needs streetlights. Wouldn’t it be easier if you had pre‑built Lego sets you could reuse?

That’s exactly what Terraform modules are: reusable building blocks that let you package infrastructure patterns and use them again and again.

Modules transform Terraform from a simple script into a scalable architecture framework, enabling teams to standardize, reuse, and share infrastructure code across environments.


What is a Module?

  • A module is a container for multiple Terraform resources that are used together.
  • It can be:
    • Local: Defined in your project folder.
    • Remote: Pulled from Terraform Registry, GitHub, or private repositories.
  • Think of modules as reusable Lego sets for infrastructure.

Why Use Modules?

  • Reusability: Write once, use everywhere (e.g. VPC setup, EC2 instances).
  • Maintainability: Centralize changes - update one module, all environments benefit.
  • Consistency: Standardize infrastructure patterns across teams.
  • Scalability: Manage complex infra by breaking it into smaller, reusable pieces.
  • Collaboration: Share modules across teams for faster onboarding.

Real‑World Example

Without modules:

resource "aws_instance" "web1" { ... }
resource "aws_instance" "web2" { ... }
resource "aws_instance" "web3" { ... }
  • Lots of duplication, hard to maintain.

With modules:

module "web_servers" {
  source        = "./modules/ec2"
  instance_count = 3
  instance_type  = "t2.micro"
}
  • Clean, reusable, scalable.

Hands‑On Lab / Demo

Lab: Creating a Simple Module

  1. Create a folder modules/ec2.
  2. Run terraform init and terraform apply.

Use the module in root config:

module "web" {
  source        = "./modules/ec2"
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}

Add variables.tf:

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

Inside, define main.tf:

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

Pro Tips & Best Practices

  • Start modularizing once you see duplication.
  • Keep modules small and focused (single responsibility principle).
  • Document inputs/outputs for clarity.
  • Use Terraform Registry for community‑tested modules.
  • Version control modules for stability.

Summary & Cheatsheet

  • Modules = Reusable building blocks.
  • Benefits: Reuse, maintainability, consistency, scalability, collaboration.
  • Structure: main.tf, variables.tf, outputs.tf.
  • Command: module "name" { source = "..." }.
Quick mnemonic: Modules = Package, Reuse, Scale

The Hackers Notebook

Modules are the secret sauce of Terraform scalability. They let you move from repetitive scripts to reusable infrastructure blueprints, empowering teams to collaborate and innovate faster. By mastering modules, you’re stepping into professional Terraform engineering.


Tips, Tricks, Roadmaps, Resources, Networking, Motivation, Guidance, and Cool Stuff ♥

Updated on Dec 31, 2025