Terraform modules explained in plain terms: a module is a reusable package of Terraform configuration, a folder of .tf files with inputs and outputs, that lets you define a piece of infrastructure once and use it everywhere. If you build the same kind of resource more than once, a module is how you stop copying and pasting it. The central question is how modules actually work, and the short answer is that a module is just a directory of configuration files: you call it from another piece of Terraform, pass in values for its variables, and get back its outputs.
Every Terraform configuration you have ever written is already a module. The files in the directory where you run plan and apply form the root module, and any other directory you call from it is a child module. There is no special syntax to learn and no separate tool to install; the distinction is simply whether a directory is run directly or called by another.
Modules are what turn Terraform from a way of writing infrastructure into a way of sharing it. A team that builds good modules stops arguing about how a database should be configured, because the decision is made once, in the module, and reused by everyone. Our guide to GitOps, where Git becomes the source of truth for infrastructure shows the workflow that modules fit into.
Key takeaways
- A module is a folder of .tf files with input variables and outputs; every configuration is already a module.
- The root module is the directory you run; child modules are the directories it calls.
- Modules encode decisions once, so every environment gets the same standard by default.
- Keep modules focused on one thing, with few variables and clear outputs.
- Version modules and pin the version in each caller, so upgrades roll out deliberately.
What is a Terraform module?
A module is a container for multiple resources that are used together. A module for a virtual private cloud, for example, might create the network, the subnets, the route tables and the security groups as one unit. The caller does not need to know about those resources: it passes in a few values, such as the network range and the environment name, and receives outputs such as the network ID.
The three files every module should have are main.tf for the resources, variables.tf for the inputs, and outputs.tf for the values it returns. That structure is a convention, but it is followed so widely that anyone reading your module will expect it.
Variables are the module’s interface. Each one has a type, a description and usually a default. Good variables are few and meaningful: the values a caller genuinely needs to choose. A module with fifty variables has no clear interface, and callers will guess at half of them.
Best for: any pattern you build more than once, such as a network, a database, a storage bucket or a standard service, where the same decisions should apply every time.
Local modules and registry modules
A local module lives in the same repository as the code that calls it, referenced by a relative path. Local modules are the right starting point: they are easy to change, easy to test, and they keep the module next to the code that uses it. Most teams begin with local modules and only move to a registry when a module is needed by several repositories.
A registry module is published to a registry, such as the public Terraform Registry or a private registry, and referenced by name and version. The registry gives you versioning and discovery: teams can find the module, read its documentation, and pin a specific version. The cost is the publishing step and the slower feedback loop, because changing the module means publishing a new version before the caller can use it.
Git is a third option: a module can be referenced directly from a repository and a tag or branch. This is a practical middle ground for teams that want versioning without running a registry. Pin the reference to a tag or a commit, never to a branch, because a branch moves and a moving reference means the module can change underneath a caller without warning.
The choice between local and published modules is really a question about who uses the module. One repository, one team: local. Several repositories, several teams: published and versioned.
How to call a module
Calling a module is a single block in your configuration. You give the block a name, set the source to the module’s location, and pass values for the variables it needs. The block’s name is how you refer to the module’s outputs elsewhere in your configuration, for example to pass a network ID into a service that runs inside it.
The source can be a local path, a registry address, or a Git URL. For a local module the source is a relative path such as ./modules/vpc. For a registry module it is an address such as hashicorp/consul/aws, plus a version constraint.
One rule worth following: a module should not contain a provider block. The caller configures the provider, and the module inherits it, which keeps regions and credentials in one place. A module that sets its own provider forces every caller to use the same region and credentials.
Another rule: a module should declare a minimum provider version rather than an exact one. The root module decides the exact version, and a child module that pins tightly makes upgrades impossible.

Composing modules into a system
Real infrastructure is built by composing modules. A root module for an environment calls a network module, passes the network’s outputs into a database module, and passes both into a service module. The root module is the composition layer: it holds almost no resources of its own, and its job is to wire modules together with the right values for that environment.
This composition is what makes environments consistent. Dev, staging and production call the same modules with different variables: smaller sizes in dev, larger ones in production. The structure is identical, so a difference between environments is a deliberate choice expressed as a variable, not an accident of copy-paste.
Composition also limits the blast radius. Each environment is a separate root module with its own state file, so a change to dev cannot reach production. A module change rolls out environment by environment, because each environment pins its own module version. Our guide to choosing the right cloud deployment model covers how these environments map to cloud accounts.
Keep the composition shallow. A root module that calls a module that calls a module that calls a module is hard to trace when something goes wrong. One or two levels of nesting is enough for most systems.
Modules compared with other reuse options
| Option | How it reuses code | Versioning | When to use it |
|---|---|---|---|
| Copy-paste | Duplicate the configuration | None: copies drift apart | Never, beyond a first experiment |
| Local module | Call a folder in the same repository | Same as the repository | One team, one repository |
| Git-sourced module | Reference a repository and tag | By tag or commit | Several repositories, no registry yet |
| Registry module | Publish and reference by name and version | Semantic versions | Shared across teams and repositories |
| Terragrunt | Wraps Terraform with shared configuration | By module version | Many similar environments with heavy repetition |
Choose the module source that matches how widely the module is shared, and revisit the choice as the team grows.
Common module mistakes
The most common mistake is a module that does too much. A module that creates a whole application platform, with fifty inputs and a dozen resources, is hard to test, hard to upgrade and hard to understand. Callers will avoid it, or use it wrongly, and the team will drift back to copy-paste. Split large modules into focused ones, each doing one thing well.
The second mistake is a module with no outputs. If a module creates resources but returns nothing, its callers cannot wire those resources into anything else, and the module becomes a dead end. Think about what the caller needs next: the ID, the address, the name.
The third mistake is an unpinned module source. A caller that references a branch, or a registry module with no version constraint, will silently pick up a new module version on the next run. That is how a routine apply suddenly changes behaviour. Pin every module reference to a version, a tag or a commit, and upgrade deliberately.
The fourth mistake is putting environment-specific logic inside a module. A module should not know whether it is running in dev or production; it should accept variables and behave the same way in both.
Finally, do not skip documentation. A module without a README, without variable descriptions and without an example call will not be reused, because nobody can tell what it does or how to call it.
Testing and maintaining modules
Test your modules. Terraform’s built-in test framework lets you write assertions about what a module creates, using mock providers so the tests run without creating real resources. Run those tests in the pipeline, so a broken module is caught before it is published or shared.
Keep a changelog for published modules, and follow semantic versioning: a major version for breaking changes, a minor version for new features, a patch version for fixes. Callers deciding whether to upgrade need to know what changed and whether their code will break. A version number without a changelog is a guess, not a signal.
Review module usage regularly. A module that nobody calls is dead code, and a module that everyone calls with the same values has variables that should be defaults. Prune, split and simplify as the system evolves.
Terraform module checklist
- One module per coherent pattern, such as a network, a database or a bucket.
- main.tf, variables.tf and outputs.tf in every module, with a README.
- Few, meaningful variables, each with a type and a description.
- Outputs for every value a caller might need next.
- No provider block inside the module; providers are passed in by the caller.
- Minimum provider version declared, never an exact pin.
- Module source pinned to a version, tag or commit in every caller.
- No environment-specific logic inside the module.
- Tests in the pipeline using the built-in test framework.
- Semantic versioning and a changelog for every published module.
Frequently asked questions
What is a Terraform module?
A module is a folder of Terraform configuration files with input variables and outputs, called from another configuration so the same infrastructure can be defined once and reused. Every configuration is a module: the directory you run is the root module, and the directories it calls are child modules.
What is the difference between a module and a resource?
A resource creates one thing, such as a single storage bucket. A module groups several resources that are used together, such as a network with its subnets and route tables, behind one interface of variables and outputs.
Should I use local modules or a registry?
Use local modules when one team and one repository use the module. Publish to a registry, or reference a Git tag, when several teams or repositories need it, because versioning and discovery then matter more than the faster feedback loop of a local module.
How do I pass values into a module?
Set the module’s input variables in the module block that calls it. Inside the module, those values are available as variables, and the module returns values to the caller through its outputs.
Can a module call another module?
Yes, and composition is how real infrastructure is built: a root module calls a network module, then passes its outputs into a database module and a service module. Keep the nesting shallow, one or two levels, so the wiring stays easy to trace.







