What Are Terraform Modules
You'll learn: What Terraform modules are and how they package reusable infrastructure configurations.
The Core Idea
A Terraform module is a container for multiple resources that are used together. Think of it like a recipe: instead of listing individual ingredients and steps every time you bake a cake, you package them into a single "chocolate cake recipe" that you can reuse whenever needed.
Every Terraform configuration you've written so far has actually been a module—the root module that lives in your working directory. Now you're ready to create additional, reusable modules to organize and standardize your infrastructure.
Why Modules Matter
Modules let you:
- Encapsulate logic: Group related resources (like a VPC with subnets, security groups, and routing tables) into a single, coherent unit
- Reuse configurations: Write once, deploy many times across environments or projects
- Simplify complexity: Hide implementation details behind a clean interface of input variables and outputs
- Standardize patterns: Ensure consistency in how resources are configured across your organization
How Modules Work
A module is simply a directory containing .tf files. It can have its own variables (inputs), resources, data sources, and outputs. When you use a module, you pass in values for its variables and receive outputs you can reference elsewhere.
Instead of copying and pasting the same resource blocks, you create a module once and call it multiple times with different input values—similar to how you use count or for_each, but at a higher organizational level.
Key Takeaway: Modules are reusable, self-contained packages of Terraform configuration that encapsulate resources and logic, making your infrastructure code more organized, maintainable, and consistent.