π§ 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)