Module composition and compatibility
Compose modules through explicit values
Dependency inversion means the consumer accepts a dependency as an input rather than deciding how to create or locate that dependency. A record module can accept metadata supplied by the root, whether the root computed it, read it from an approved source, or received it from another module. This makes reuse possible without secretly managing a second copy of shared infrastructure.
Build a consumer and connect the modules
Create modules/record/main.tf with this complete child configuration. It creates only a local synthetic record.
variable "metadata" { description = "Naming metadata supplied by the caller." type = object({ name = string tags = map(string) }) nullable = false}
resource "terraform_data" "record" { input = var.metadata}
output "record_name" { value = terraform_data.record.output.name}The following root main.tf connects the naming module from the preceding lesson to the consumer.
terraform { required_version = "= 1.14.8"}
module "naming" { source = "./modules/naming" service = { name = "catalog" environment = "dev" tags = { owner = "learning" } }}
module "record" { source = "./modules/record" metadata = module.naming.metadata}
output "record_name" { value = module.record.record_name}The root passes the output explicitly. Terraform infers dependencies from the references; file names do not enforce execution order. The expected record_name after a reviewed local apply is dev-catalog. The naming module itself has no managed resource. The one expected resource address is module.record.terraform_data.record. A different caller can supply a literal object of the same accepted type without invoking the naming module.
Choose stable boundaries and keys
Prefer a shallow root that connects focused modules. Nested child modules are useful if the abstraction owns those implementation details, but unnecessary nesting makes addresses and migrations harder to follow. For repeated independent services use meaningful keys, not list positions.
module "records" { source = "./modules/record" for_each = { catalog = { name = "dev-catalog", tags = { environment = "dev" } } search = { name = "dev-search", tags = { environment = "dev" } } } metadata = each.value}This independent root illustration would create addresses under module.records["catalog"] and module.records["search"]. Changing the value for catalog keeps the key; renaming the key changes the instance address. Keep cloud provider configurations in the root where practical, and declare each child’s provider requirements separately when it needs external providers. Aliases are covered next. This example uses only the built-in provider.
Preserve callers during interface upgrades
Suppose consumers rely on metadata.name. Removing metadata or renaming name to display_name breaks callers. A transition can introduce a new output while keeping the old one until all consumers migrate.
# Inside the naming module: retain the existing output during transition.output "display_name" { value = local.name}
output "metadata" { value = { name = local.name tags = local.tags }}The example replaces the existing metadata declaration with an equivalent one and adds display_name; do not paste a duplicate metadata block. Existing callers continue to receive the same object. Document the deprecation, test old and new consumers, and release a breaking removal only after a coordinated migration. A new required input, a narrowed validation rule, or a changed default can also break callers or change plans.
Preserve resource addresses during refactoring
If the module internally renames terraform_data.record to terraform_data.entry, change its output reference too. Declare an explicit move in that child module.
# Inside modules/record, after renaming the resource to entry:moved { from = terraform_data.record to = terraform_data.entry}Terraform can associate existing state with the new address rather than treating the rename alone as a new object. Keep historical moves for supported upgrade paths. A moved block does not guarantee a no-change plan if other arguments changed. Review the upgrade against existing disposable state before approving it. Never edit state JSON manually to perform this rename.
Test the interface and upgrade
In a fresh approved local copy, run the following commands manually. No cloud account, remote module, or external provider is used. Commands and expected results are unexecuted.
terraform initterraform fmt -recursiveterraform validateterraform planExpect one local create. Optionally apply only a reviewed plan; inspect record_name and confirm dev-catalog. Change the name to search and review the plan before applying. For a separate upgrade test, keep the same input and state, rename the child resource and its output reference, add the moved block, and verify that the rename alone does not propose deletion or creation. Review a destroy plan for the one lab record before optional cleanup. Keep state private and do not use an existing project. For offline practice, use the supplied cases and predict which changes are compatible.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Module composition and compatibility — knowledge check
Review interfaces, composition and upgrade safety.
How should a record module receive shared naming metadata?
Which address has a stable key in the keyed example?
How can you introduce display_name without breaking metadata callers?
What is the purpose of the moved block in the child?
What evidence should precede approving an upgrade?