Module Structure and Organization
What you'll learn: How to organize the files and folders inside a Terraform module for clarity, reusability, and maintainability.
Why Structure Matters
You've learned what Terraform modules are—now it's time to organize them properly. A well-structured module is like a well-organized toolbox: everything has its place, making it easy to find what you need and understand how pieces fit together.
Standard Module Layout
Terraform modules follow a conventional directory structure that makes them predictable and easy to navigate:
my-module/
├── main.tf # Primary resource definitions
├── variables.tf # Input variable declarations
├── outputs.tf # Output value definitions
└── README.md # Module documentation
The Core Files
main.tf contains your primary resource blocks and data sources. Think of it as the "engine room" where the actual infrastructure is defined.
variables.tf holds all variable declarations—your module's "input panel" where users configure behavior without touching internal logic.
outputs.tf defines what information the module exposes to the outside world—like a dashboard showing key values after resources are created.
README.md documents what the module does, what variables it accepts, and what it outputs. Essential for anyone (including future you) using the module.
Why Separate Files?
Splitting concerns into dedicated files makes modules scannable. When someone needs to know "what can I configure?" they look at variables.tf. When they ask "what will this give me back?" they check outputs.tf. No hunting through thousands of lines.
Key Takeaway: Following the standard module structure (main.tf, variables.tf, outputs.tf, README.md) creates predictable, maintainable modules that others can understand and use immediately.