Root and child modules
Reuse an infrastructure pattern
A module groups Terraform configuration files in a directory. Imagine that two catalog environments need the same metadata structure, but different names. Instead of duplicating the resource definition, put it in a child module and call that module twice. The configuration from which Terraform starts is the root module. A child module is a module used by another module. Both calls in this lesson belong to one root configuration and one selected state; a module is not a workspace or an access-control boundary.
Create the local module
Create a fresh disposable directory with the following layout. All examples use the inherited provisional Terraform 1.14.8 pin; it has not been executed or verified here. No cloud account, backend or external provider is needed.
module-lab/ main.tf modules/ label/ main.tfPlace the following in modules/label/main.tf. The variable is the public input and the output is the public result. The resource uses the built-in terraform_data type introduced earlier; it keeps a synthetic record, not a cloud object.
variable "name" { type = string}
resource "terraform_data" "record" { input = { name = var.name team = "catalog" }}
output "metadata" { value = terraform_data.record.output}The module does not have to put variables and outputs in separate files. Larger modules often do so for readability. A nested directory is not automatically included just because it exists.
Call the module twice
Put this configuration in the root main.tf. The module labels distinguish calls; source identifies the implementation; name supplies each call with its own value. The root outputs expose selected child outputs.
terraform { required_version = "= 1.14.8"}
module "dev" { source = "./modules/label" name = "dev-catalog"}
module "test" { source = "./modules/label" name = "test-catalog"}
output "dev_metadata" { value = module.dev.metadata}
output "test_metadata" { value = module.test.metadata}The expressions module.dev.metadata and module.test.metadata access outputs, not arbitrary internal attributes. A child cannot read a root variable simply because the variable has the same name; pass needed values explicitly. Each call creates its own instance of the resource in the module tree.
Read the dependency graph and addresses
The resource in each child depends on that call’s name input. The root output depends on the corresponding child output, which in turn references the resource. There is no dependency between dev and test in this example. Terraform may process independent nodes concurrently; textual order does not impose execution order. The expected resource addresses are:
module.dev.terraform_data.recordmodule.test.terraform_data.recordThe two addresses are different even though both resources are named record in their own module. Do not manage the same real object from two calls. Renaming a module label changes resource addresses and can propose destruction and creation unless a suitable state migration is declared and reviewed. Refactoring is covered later.
Initialize, inspect and verify
In the fresh fixture, ensure the selected binary, working directory and local-only context are approved. Run these commands manually; they have not been run here.
terraform initterraform fmt -check -recursiveterraform validateterraform planExpect two creates in an empty state and no cloud operations. The exact plan text and IDs can vary. After reviewing a correct plan, an optional local apply should produce two root output objects with names dev-catalog and test-catalog and team catalog. Inspect with terraform output and terraform state list. A second plan should have no changes if inputs and configuration are unchanged. For cleanup, review terraform plan -destroy and approve terraform destroy only for these two local records. Never run these instructions in an existing project. No state or saved plan is shipped.
Practice the interface
Add a third call named preview with input preview-catalog. Predict the new address and output before planning. If you have not run the lab, answer from the configuration instead. Explain why adding another call does not create another state file, and why changing a child output can affect callers even if the underlying resource is unchanged.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Root and child modules — knowledge check
Review module calls, addresses and source selection.
What makes a directory a child module in a configuration?
How does a root access the metadata output of the dev call?
What happens when the same local module is called twice with distinct labels?
Which address belongs to the test call?
What happens to state isolation when a child module is added?