- Explain what a Terraform module is and why modules are used.
- Distinguish the root module (your working directory) from child modules called by the root module.
- Describe a module’s typical structure and the roles of
main.tf,variables.tf, andoutputs.tf. - Call and consume a module using inputs and outputs.
- Compare configurations with and without modules and reason about trade-offs for reusability, consistency, and maintainability in enterprise Azure deployments.
A Terraform module is simply a folder containing Terraform configuration files. The root module is the configuration in your current working directory; any other folder referenced with a
module block is a child (or remote) module.Root module vs child modules
- Root module: The Terraform configuration in the directory where you run
terraform initandterraform apply. It can call other modules and orchestrate resources. - Child module: Any module invoked by the root module using a
moduleblock. Child modules can be local folders, Git repositories, or modules published to the Terraform Registry.
storage:
Typical module structure
Modules are usually organized with a small set of well-known files. Following this convention improves clarity, reuse, and versioning. Directory layout example:
Minimal example variables for a storage module:
Calling and consuming a module
In your root module, use amodule block to call a child module and map inputs. Then reference module outputs with module.<NAME>.<OUTPUT>.
Example root invocation and output consumption:
Why use modules?
Modules provide several concrete benefits for teams and enterprise Azure deployments:
Design guidance: aim for small, single-responsibility modules with clear input/output contracts. Use inputs to parameterize behavior and outputs to expose only what callers need.
Comparing configurations: with vs without modules
Best practices and considerations
- Keep modules focused (single responsibility) and parameterized through clear inputs.
- Avoid hardcoding environment-specific values in modules; supply them from the root module or environment variables.
- When publishing modules, include
versions.tfand clear README documentation. - Be deliberate about provider configuration: prefer configuring providers in the root module and passing provider aliases to child modules when needed.
- Use remote state or workspaces appropriately to manage state separation between environments.
Related topics and references
- Terraform Modules Documentation
- Azure Provider for Terraform
- Terraform Registry
- Best practices for module design
- Structuring module inputs and sensible defaults for flexible reuse.
- Publishing and versioning modules: local vs registry vs Git.
- Provider management and remote state strategies across modules.
- Real-world Azure patterns for enterprise: networking, identity, and security modules.