Composing Configuration with Modules
Most configurations combine several modules. This lesson covers calling modules, wiring them together, the module meta-arguments and the structure of reusable and published modules.
Learning outcomes
After completing this lesson, you will be able to:
- Describe module composition and the module meta-arguments.
- Identify the standard module structure and registry publishing rules.
Calling and wiring modules
Module composition means building infrastructure from small, focused modules that the root module connects by passing outputs into inputs. HashiCorp recommends keeping the module tree flat: the root module calls each module directly and does the wiring, rather than modules calling modules many levels deep.
module "network" {
source = "terraform-aws-modules/vpc/aws"
version = "~> 6.0"
name = "app"
cidr = "10.0.0.0/16"
azs = ["eu-west-1a", "eu-west-1b"]
private_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
}
module "database" {
source = "./modules/database"
subnet_ids = module.network.private_subnets
}The reference to module.network.private_subnets creates a dependency, so Terraform creates the network first. Every resource inside a module has the module's name in its address, such as module.network.aws_vpc.this[0], and the plan groups resources by module.
Module meta-arguments
Module blocks accept four meta-arguments in addition to source and version. The following table describes them.
| Meta-argument | Effect |
|---|---|
| count | Create several instances of the whole module, addressed module.NAME[0] |
| for_each | Create one module instance per map key or set element, addressed module.NAME["key"] |
| providers | Pass provider configurations, including aliases, to the module |
| depends_on | Make every object in the module wait for the listed objects |
module "bucket" {
source = "./modules/bucket"
for_each = toset(["logs", "reports", "backups"])
name = "acme-${each.key}"
}
output "bucket_arns" {
value = { for k, m in module.bucket : k => m.arn }
}A module that contains its own provider blocks cannot be used with count, for_each or depends_on, which is one reason reusable modules should receive provider configurations from the caller.
The following diagram shows one module block creating three instances with for_each.
![One module block with for_each instead of three copies. A map of environment to instance type creates module.app[“dev”], [“test”] and [“prod”], each receiving each.value. If the environments need separate state, access controls or approvals, separate root configurations or HCP Terraform workspaces fit better.](/media/page_components/diagram-1e3fb6dd222821b79562cc2b.webp)
Module meta-arguments
Standard module structure
HashiCorp defines a standard module structure that tools and the registry understand. Only the root directory's .tf files are required; the rest is strongly recommended.
terraform-aws-bucket/
├── README.md
├── LICENSE
├── main.tf
├── variables.tf
├── outputs.tf
├── versions.tf
├── modules/
│ └── replication/
└── examples/
└── basic/
└── main.tfTo publish to the public Terraform Registry, the module must be in a public GitHub repository named terraform-<PROVIDER>-<NAME>, such as terraform-aws-bucket, have a description, use the standard structure and have at least one release tag in semantic version form, such as v1.0.0. Each new tag becomes a new version. The HCP Terraform private registry publishes modules for one organisation, from a VCS repository or through the API.
- Module composition
- Building a configuration from small modules connected through outputs and inputs in the root module.
- Standard module structure
- main.tf, variables.tf, outputs.tf and README.md, with optional modules/ and examples/ directories.
- Registry naming
- terraform-<PROVIDER>-<NAME> repositories, released with semantic version tags.
Publishing to the public registry
One module, three environments
A team needs the same application stack in dev, test and prod, with different instance sizes, in one configuration. A developer copies the module block three times. Is there a better approach?
The decisive feature is for_each on module blocks.
A map variable such as environments = { dev = "t3.micro", test = "t3.small", prod = "m6i.large" } with for_each = var.environments on one module block creates three instances, module.app["dev"] and so on, each receiving instance_type = each.value. If the environments need separate state, access controls or approval processes, separate root configurations or HCP Terraform workspaces are a better fit than one configuration.
How must a repository be named to publish a module to the public Terraform Registry?
Show answer
terraform-<PROVIDER>-<NAME>, such as terraform-aws-vpc, with semantic version release tags.
Exam Tips
Remember for the exam
- Compose small modules in the root module; keep the tree flat.
- Module meta-arguments: count, for_each, providers, depends_on.
- Standard structure: main.tf, variables.tf, outputs.tf, README.md.
- Public registry repos are named terraform-<PROVIDER>-<NAME> with semantic version tags.