Skip to main content
Welcome to the lesson on Terraform modules. As infrastructure grows—especially in Azure environments—Terraform configurations often become repetitive and harder to manage. Terraform modules solve this by grouping related resources into reusable, composable units. This lesson builds both conceptual and practical understanding of modules: what they are, how they’re structured, how to call them, and when to use them. Learning objectives
  • 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, and outputs.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.
This overview introduces core concepts with minimal, focused examples to make ideas concrete and actionable.

Root module vs child modules

  • Root module: The Terraform configuration in the directory where you run terraform init and terraform apply. It can call other modules and orchestrate resources.
  • Child module: Any module invoked by the root module using a module block. Child modules can be local folders, Git repositories, or modules published to the Terraform Registry.
Example: a root module that calls a child module named 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:
Common file responsibilities: Minimal example variables for a storage module:
Example module resources:
Example outputs:

Calling and consuming a module

In your root module, use a module block to call a child module and map inputs. Then reference module outputs with module.<NAME>.<OUTPUT>. Example root invocation and output consumption:
Common commands to execute from the root module directory:

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.tf and 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.
Further exploration
  • 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.
Now that you understand what Terraform modules are and why they matter, proceed to the hands-on example and step-by-step module build in the next lesson.

Watch Video