module block in Terraform.
This article is a concise, practical introduction to modules and how the module block helps you organize reusable infrastructure as code. Modules let you group related resources into composable building blocks so you can reuse, version, and share tested infrastructure patterns across projects and teams.
What is a Terraform module?
A Terraform module is a collection of resources, variables, and outputs that together implement a logical piece of infrastructure (for example: networking, databases, or a Kubernetes cluster). You call a module using amodule block from another configuration to instantiate that set of resources with specific input values.
Modules reduce duplication and make infrastructure easier to manage:
- Reusability: Share standard patterns across teams.
- Maintainability: Break large configurations into smaller, focused pieces.
- Consistency: Versioned modules enforce tested configurations.

module blocks, pass the required inputs, and avoid duplicating resource definitions.

How the module block works
Themodule block calls a module source and injects it into your current configuration. You provide inputs (variables) to configure the module and the module returns outputs that other parts of your configuration can use.
Common use cases:
- Reusing a network module across multiple environments with different CIDR blocks.
- Sharing a standardized database cluster setup across several applications.
- Composing modules together to form complex architectures without repeating low-level resource definitions.

Example: Using a VPC module from the Terraform Registry
Below is a typical example using a popular AWS VPC module from the Terraform Registry. Instead of creating many networking resources directly, you call a tested module and pass the values you need.source— Where the module code lives (a Registry module in this case).version— Pins a specific module release to ensure reproducible runs.- Inputs such as
name,cidr, and the subnet lists correspond to variables declared inside the module and let you customize its behavior.
Common module block attributes
| Attribute | Purpose | Example |
|---|---|---|
source | Location of the module code. Can be a Registry address, Git URL, local path, or other supported source. | terraform-aws-modules/vpc/aws |
version | (Registry modules) Pin the module version to ensure reproducible deployments. | "4.0.1" |
| inputs (arguments) | Values you pass to module variables to configure the module. | name = var.vpc_name |
| outputs | Values the module exports so other resources can consume them. | module.vpc.public_subnets |
Reuse patterns
- Call the same module multiple times with different arguments to create multiple instances (e.g., separate VPCs for different environments).
- Use
for_eachorcountwith module blocks to dynamically create multiple module instances when supported (Terraform 0.13+ supports loop over modules). - Version pin modules and review changes before upgrading to avoid surprises in production.
Best practices
- Keep module interfaces small and stable: expose only the variables and outputs needed.
- Pin module versions and test upgrades in a staging environment.
- Publish commonly used modules to a private module registry or the Terraform Registry for easy discovery and reuse.
- Document module inputs, outputs, and behavior so other teams can consume them correctly.
Pinning a module version ensures reproducible deployments. Test new module versions deliberately — do not rely on automatically pulling the latest release.
Quick reference and further reading
- Terraform Modules documentation: https://www.terraform.io/docs/language/modules/index.html
- Terraform Registry (modules): https://registry.terraform.io/
- Best practices for reusable modules: https://www.terraform.io/docs/language/modules/develop/index.html