What is a module?
A module is a container for related Terraform resources that are commonly used together — for example, the resources that make up a subnet, a VPC, or an application stack. Modules are the recommended way to package and reuse configurations. When you run Terraform, the configuration in the working directory is the root (or parent) module. Any other module called from the root module is a child (reusable) module. Using modules helps encapsulate complexity and standardize patterns.The root module is the directory where you run Terraform commands. Child modules live in other directories, remote registries, or version control and are referenced via
module blocks.Benefits of using modules
Using modules delivers several operational and organizational advantages:- Improved organization: Break large configurations into smaller, easier-to-reason units.
- Easier collaboration: Publish vetted modules to a registry or shared repo so teams consume standardized building blocks.
- Consistent patterns: Centralize naming, tagging, sizing, and security controls in modules to enforce best practices.

Modules as building blocks
Instead of placing many resource blocks (TLS certificate, load balancer, message queue, Kubernetes cluster, database, etc.) into a single configuration, define each piece once as a module and reuse it across multiple root modules. For example:- A Marketing Application might reference the TLS and Load Balancer modules.
- A GenAI service might reference those plus GPU-backed cluster and specialized queue modules.

What does a module look like?
A module is simply a directory containing standard Terraform files. The most common layout is:main.tf— resources and core configurationvariables.tf— input variables the module acceptsoutputs.tf— outputs the module exposes to callers- (optional)
versions.tf— provider and Terraform version constraints - (optional)
examples/— example usage to help consumers
Example: calling a local child module from a root module
variables.tf inside the module:
outputs.tf inside the module:
Module sources and versioning
Modules can be sourced from several places: local paths, Git repositories, Terraform Registry, or other VCS. Always prefer versioned sources for reproducible builds when pulling remote modules. Examples of module sources
When referencing remote modules, pin to a specific tag, branch, or commit using
?ref= to avoid accidental changes.
Declaring and using module blocks
To reuse a module, declare amodule block in your root module and set the source and any required inputs. The root module receives outputs from the child module as attributes.
Example with multiple modules:
- Keep modules focused and single-purpose.
- Use clear, descriptive variable names and document defaults.
- Provide outputs that callers need without exposing internal resource IDs unnecessarily.
- Include examples and tests (e.g., Terratest or Kitchen-Terraform) where feasible.
- Apply provider and Terraform version constraints in
versions.tf. - Use semantic versioning and tag releases for remote modules.
Summary
Modules are the primary mechanism in Terraform for packaging, sharing, and enforcing infrastructure patterns. Use them to:- Reduce duplication
- Improve maintainability
- Standardize security and operational controls
Links and References
- Terraform Documentation: Modules
- Terraform Registry
- Terraform CLI: Module Sources
- Semantic Versioning