TechyPilots Info
TechyPilots.INFO
Home/Tutorials/Terraform/Terraform Modules
Terraform Fundamentals

Terraform Modules

Learn how Terraform modules help you organize, reuse, and manage infrastructure configurations using modular Infrastructure as Code.

Level: Intermediate
Topic: Terraform Modules

Terraform Module Flow

Root Module → Child Module → Infrastructure

Create Reusable Module

Define Module Variables

Create Infrastructure Resources

Expose Module Outputs

Call Module from Root Configuration

Introduction

Organize your infrastructure with modules

Terraform modules allow you to group related infrastructure resources into reusable components.

Instead of keeping every resource inside one large Terraform configuration, modules allow you to separate infrastructure into smaller logical components.

A module can represent a web server, network, Kubernetes cluster, database, or any other reusable infrastructure component.

Core Concepts

Understanding Terraform modules

These concepts form the foundation for building reusable and well-organized Terraform infrastructure.

01

Root Module

The root module is the main Terraform configuration directory where Terraform commands are executed.

Key Topics

  • Main configuration
  • Provider configuration
  • Module calls
  • Infrastructure orchestration
02

Child Modules

Child modules are reusable Terraform configurations that can be called from a root module or another module.

Key Topics

  • Reusable resources
  • Input variables
  • Output values
  • Module organization
03

Module Inputs

Input variables allow a module to receive configuration values from the module that calls it.

Key Topics

  • Variables
  • Configuration values
  • Customization
  • Environment settings
04

Module Outputs

Output values allow a module to expose useful information to the root module or other parts of the configuration.

Key Topics

  • Resource IDs
  • IP addresses
  • DNS information
  • Infrastructure values

Module Structure

Organize reusable Terraform components

A typical Terraform project can contain a root module and one or more child modules.

Each module can contain its own resources, variables, and outputs inside a dedicated directory.

Example Project Structure

terraform-project/
│
├── main.tf
├── variables.tf
├── outputs.tf
│
└── modules/
    │
    └── web-server/
        ├── main.tf
        ├── variables.tf
        └── outputs.tf

Child Module Resource

resource "aws_instance" "web_server" {
  ami           = var.ami_id
  instance_type = var.instance_type

  tags = {
    Name        = var.server_name
    Environment = var.environment
  }
}

Module Resources

Define infrastructure inside a module

A child module can contain one or more Terraform resources just like a normal Terraform configuration.

The difference is that the module can receive values through input variables, making the infrastructure reusable across multiple deployments.

Module Variables

Customize reusable infrastructure

Modules use input variables to receive values from the root module or another calling module.

This allows one module to support different configurations without modifying the internal resource definitions.

Module Input Variables

variable "ami_id" {
  description = "AMI ID for the server"
  type        = string
}

variable "instance_type" {
  description = "EC2 instance type"
  type        = string
}

variable "server_name" {
  description = "Name of the server"
  type        = string
}

variable "environment" {
  description = "Deployment environment"
  type        = string
}

Calling a Terraform Module

module "web_server" {
  source = "./modules/web-server"

  ami_id        = "ami-example"
  instance_type = "t3.micro"

  server_name = "web-server"

  environment = "development"
}

Module Blocks

Use reusable modules from your root configuration

A Terraform module is called using a module block. The source argument tells Terraform where the module is located.

The root module can provide values for the child module input variables when calling the module.

Module Outputs

Expose infrastructure information

Modules can expose useful resource information using Terraform output blocks.

The root module can reference these outputs using the module name followed by the output name.

Module Output Example

output "instance_id" {
  description = "ID of the web server"

  value = aws_instance.web_server.id
}

output "public_ip" {
  description = "Public IP address"

  value = aws_instance.web_server.public_ip
}

Benefits

Why use Terraform modules?

Reusable Infrastructure

Create infrastructure components once and reuse them across multiple environments and projects.

Better Organization

Modules help divide large Terraform configurations into smaller and easier-to-manage components.

Consistent Deployments

The same module can be used to create consistent infrastructure across development, staging, and production.

Simplified Maintenance

Infrastructure changes can often be managed centrally within a reusable module.

Complete Example

Use a module from the root configuration

The root module can call a child module, provide input values, and use output values from the created infrastructure.

# Root Module

module "web_server" {
  source = "./modules/web-server"

  ami_id        = "ami-example"
  instance_type = "t3.micro"

  server_name = "production-web"

  environment = "production"
}

output "web_server_ip" {
  value = module.web_server.public_ip
}

Learning Roadmap

Build your first Terraform module

Step 1

Understand Terraform Modules

Learn how Terraform uses modules to organize and reuse infrastructure configurations.

Step 2

Create a Module Directory

Create a dedicated directory containing the Terraform resources for a reusable component.

Step 3

Define Module Variables

Add input variables that allow the module to receive configuration values.

Step 4

Create Resources

Define the infrastructure resources that the module will manage.

Step 5

Create Module Outputs

Expose useful infrastructure information using output values.

Step 6

Call the Module

Use a module block from the root configuration to create infrastructure from the reusable module.

Best Practices

Working with Terraform modules

Keep Modules Focused

Each module should have a clear purpose and manage related infrastructure components.

Use Clear Variable Names

Use descriptive input variable names that clearly explain the configuration being provided.

Document Modules

Document the purpose, variables, outputs, and expected usage of reusable modules.

Expose Useful Outputs

Create output values for infrastructure information that other modules or configurations may need.

Avoid Hardcoded Values

Use input variables to make modules flexible and reusable across environments.

Version Your Modules

Manage and track changes carefully when modules are reused across multiple infrastructure projects.

Next Step

Build reusable infrastructure with Terraform modules

Practice organizing Terraform resources into modules, defining module variables, exposing outputs, and reusing the same infrastructure components across multiple environments.