🧠 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
- VPC module outputs vpc_id
- Root module captures vpc_id
- Root module passes vpc_id into EKS module
- 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:
- Terraform is not just about resources
- Terraform is about designing systems
- 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)