DEV Community

Cover image for πŸš€ Terraform Day 20: Building Production Infrastructure Using Custom Terraform Modules
Jeeva
Jeeva

Posted on

πŸš€ Terraform Day 20: Building Production Infrastructure Using Custom Terraform Modules

🧠 What Is a Terraform Module?
A Terraform module is a reusable, self-contained package of Terraform code.

A module typically contains:
main.tf – resources
variables.tf – inputs
outputs.tf – exposed values

Terraform treats every directory as a module.

Modules allow you to:
Encapsulate complexity
Enforce standards
Reuse infrastructure patterns
Scale cleanly across teams and environments

🧩 Types of Terraform Modules
1️⃣ Public Modules
Available from Terraform Registry
Maintained by providers or the community
Cannot be modified internally

2️⃣ Partner Modules
Co-managed by HashiCorp and partners
Verified and production-ready
Still externally controlled.

3️⃣ Custom Modules (Focus of Day 20)
Created and maintained by you or your organization
Full control over:
Code
Versioning
Security
Updates
πŸ‘‰ Production systems rely heavily on custom modules.

πŸ— Root Module vs Custom Modules
πŸ”Ή Root Module
Entry point of the Terraform project
Where terraform init and terraform apply are executed
Orchestrates the entire infrastructure
Sources and connects custom modules

πŸ”Ή Custom Modules
Stored as subdirectories inside the root module
Each module has a single responsibility
Do not directly communicate with each other
The root module acts as the central coordinator.

πŸ“‚ Typical Project Structure (Day 20 Style)
terraform-project/
β”œβ”€β”€ main.tf # Root module
β”œβ”€β”€ variables.tf
β”œβ”€β”€ outputs.tf
β”œβ”€β”€ modules/
β”‚ β”œβ”€β”€ vpc/
β”‚ β”‚ β”œβ”€β”€ main.tf
β”‚ β”‚ β”œβ”€β”€ variables.tf
β”‚ β”‚ └── outputs.tf
β”‚ β”œβ”€β”€ eks/
β”‚ β”‚ β”œβ”€β”€ main.tf
β”‚ β”‚ β”œβ”€β”€ variables.tf
β”‚ β”‚ └── outputs.tf
β”‚ β”œβ”€β”€ iam/
β”‚ └── secrets/
Each module encapsulates a single infrastructure domain.

πŸ” How Modules Interact (Very Important)
Custom modules never talk to each other directly.

All communication happens through the root module:
Root module calls a custom module
Passes input variables
Custom module creates resources
Custom module exposes outputs
Root module consumes outputs
Outputs are passed into other modules as inputs

This ensures:
Loose coupling
Clear dependency flow
Maintainable architecture

πŸ”— Example: VPC β†’ EKS Dependency Flow

  1. VPC module outputs vpc_id
  2. Root module captures vpc_id
  3. Root module passes vpc_id into EKS module
  4. EKS resources are created inside the correct VPC This pattern scales cleanly as infrastructure grows.

πŸ“₯ Variables & Outputs in Modular Design
Variables
Define what a module expects
Passed from the root module
Keep modules flexible

Outputs
Define what a module exposes
Used by the root module
Enable inter-module coordination

Modules behave like functions:
Inputs β†’ Processing β†’ Outputs

🏁 Conclusion
Day 20 transitions Terraform from learning mode to architecture mode.

This session makes it clear that:

  1. Terraform is not just about resources
  2. Terraform is about designing systems
  3. Custom modules are the backbone of production IaC

By mastering modular Terraform architecture, you unlock the ability to build large, maintainable, enterprise-grade cloud platforms.

This is how Terraform is used in the real world.

Top comments (0)