Local module call
module "service" {
source = "./modules/service"
name = "api"
}
output "service_name" {
value = module.service.name
}- The caller consumes module outputs rather than reaching into child internals.
Design reusable modules with explicit interfaces, separate root and child module responsibilities, and use version constraints plus the provider dependency lock file deliberately.
Every Terraform configuration is a module. A root module can call child modules from local paths, registries or version-control sources. Reuse should encapsulate a meaningful infrastructure capability, not merely reduce line count.
Variables define accepted inputs and outputs expose intentional results. Avoid forcing callers to know internal resource addresses when a stable output can serve as the interface.
Version constraints declare acceptable versions. The dependency lock file records selected provider versions and checksums so future initialization can reproduce the same provider selection until intentionally upgraded.
module "service" {
source = "./modules/service"
name = "api"
}
output "service_name" {
value = module.service.name
}Goal: Create a reusable local module with an input, managed object and output.
mkdir -p ~/gnu-terraform-labs/module07/modules/service && cd ~/gnu-terraform-labs/module07
cat > modules/service/main.tf <<'EOF'
variable "name" {
type = string
}
resource "terraform_data" "this" {
input = var.name
}
output "name" {
value = terraform_data.this.output
}
EOFcat > main.tf <<'EOF'
module "service" {
source = "./modules/service"
name = "api"
}
output "service_name" {
value = module.service.name
}
EOFterraform init
terraform apply -auto-approveterraform output -raw service_nameInternal module registries and semantic versioning let teams reuse approved infrastructure patterns while controlling change.
Open each answer only after you have tried to answer the question yourself.
Clear inputs, intentional outputs and encapsulated implementation details.
Selected provider versions and checksums.
When provider/backend/module installation requirements change.
Progress is saved in this browser immediately. When you sign in to My Learning, it can also sync across devices.
Attempt the core lab and knowledge checks before marking the module complete.