Home DevOpsTerraform Modules Explained: Reusable Infrastructure

Terraform Modules Explained: Reusable Infrastructure

by Knowledge Base Linux
terraform modules building reusable cloud infrastructure blocks

Copy-pasting Terraform code between projects feels fast until the third copy drifts: one environment gets the new security group rule, the others do not, and nobody remembers which copy is canonical. Terraform modules end that drift by packaging a set of resources into a reusable unit with a clear interface of variables and outputs. In this guide you will build a module from scratch, wire variables and outputs correctly, consume it from multiple environments, and version it so changes roll out deliberately.

A module is simply a directory of Terraform files. Every Terraform configuration is already a module: the files in your working directory form the root module, and any directory you reference with a module block becomes a child module. The skill is not syntax but design: deciding what belongs inside a module, what should be a variable, and what the module should expose back to its caller.

terraform modules stamping out development staging production environments

How Terraform Modules Work

When you declare a module block, Terraform loads the source directory, evaluates it with the variables you pass, and creates one instance of every resource inside it. The same module can be instantiated many times with different inputs, so a single VPC module can produce the development, staging, and production networks from one codebase. Modules can come from a local path, a Git repository, or a registry, and the source address decides how the module is fetched and versioned.

Inside a module, resources reference variables with var.name and expose results with output blocks. Outside the module, callers read those results as module.name.output_name. This boundary is the whole point: callers do not need to know which resources the module creates, only what it needs and what it returns.

Prerequisites

  • Terraform 1.5 or later installed locally.
  • A cloud account or a local provider for testing; the examples use AWS-style resources but the patterns are provider-neutral.
  • Basic familiarity with Terraform resources, variables, and state.

Step 1: Build Your First Terraform Module

Create a module directory with three files. variables.tf declares the interface, main.tf creates the resources, and outputs.tf exposes the results:

# modules/web-server/variables.tf
variable "instance_type" {
  type    = string
  default = "t3.micro"
}
variable "tags" {
  type    = map(string)
  default = {}
}

# modules/web-server/main.tf
resource "aws_instance" "web" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = var.instance_type
  tags          = merge(var.tags, { Name = "web-server" })
}

# modules/web-server/outputs.tf
output "instance_id" {
  value = aws_instance.web.id
}

Give every variable a type and a description, and a sensible default only when one genuinely exists. A required variable with no default forces the caller to make a conscious choice, which is better than a silent default that provisions the wrong size in production.

Step 2: Consume the Module from Multiple Environments

Reference the module from your root configuration and pass different inputs per environment:

module "web_staging" {
  source        = "./modules/web-server"
  instance_type = "t3.micro"
  tags          = { Environment = "staging" }
}

module "web_production" {
  source        = "./modules/web-server"
  instance_type = "t3.small"
  tags          = { Environment = "production" }
}

Run terraform init after adding a module block so Terraform installs it, then terraform plan to see both instances. When the module changes, every caller picks up the change on its next apply, which is why versioning matters for shared modules.

Step 3: Version Shared Terraform Modules

For modules shared across teams, reference a Git tag or a registry version instead of a moving branch:

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

Pinning a version makes applies reproducible: the same code provisions the same infrastructure next month. Adopt semantic versioning for your own modules, bump the major version for breaking variable changes, and document the upgrade path in the module README so callers can move deliberately rather than by accident.

Troubleshooting Terraform Modules

Terraform cannot find the module. Run terraform init after changing a source address, and check that local paths are relative to the root module, not to the file containing the block.

Variables are not passed through. A variable declared in the child module but never set in the module block falls back to its default, or fails if it has none. Check the block, not the child module, when a value looks wrong.

Outputs are empty or unknown. Outputs that depend on resources not yet created show as unknown during plan; that is normal. A permanently empty output usually means the output block references the wrong resource attribute.

State gets messy after refactoring. Moving resources into a module changes their addresses. Use moved blocks to tell Terraform the resource only changed address, instead of destroying and recreating it.

Conclusion

Terraform modules turn repetitive infrastructure into a library: one well-tested definition, many environments, and changes that propagate through versioning instead of copy-paste. Start with a small module for one resource type, keep its interface narrow, and grow it only when a second caller proves the abstraction is real. For more infrastructure-as-code practice, see our guides on Terraform workspaces and Terraform remote state with S3. The official reference is Terraform modules documentation.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.