Pro ยท Lesson 6 of 12
A module is a set of files you call with inputs and read through outputs. A folder of copied files is not a module until something calls it.
Sign in to save your place.
A module is a recipe card. The root module is the meal you are cooking tonight. You can use the same card for lunch tomorrow with different ingredients. You do not photocopy the card and then edit both copies until they disagree.
The directory where you run terraform is the root module. A module block calls a child. The child's variables are that block's arguments. The child's outputs are module.name.output. Resources inside the child get addresses like module.network.aws_subnet.private.
A child module cannot see the caller's locals. Pass what it needs. Return what the caller needs. Hiding an id inside the child and then scraping state is how later refactors break.
A source can be a local path, a git URL, or a registry. Pin a version on a registry module. An unpinned source will change. terraform init downloads modules. Commit the lock file when the module source supports one.
Do not point a production root module at a branch named main with no revision. You will apply someone else's Tuesday commit by accident.
| Source | Pin it with |
|---|---|
| Local path | Code review of that folder |
| Git | A tag or a commit |
| Registry | A version argument |
Step 1 of 3The root is where you run plan.
Step 2 of 3The child receives variables and returns outputs.
Step 3 of 3The real objects get addresses that include the module name.
Two directories. The root passes environment. The child returns a name. No cloud provider.
mkdir -p modules/name
cat > modules/name/main.tf <<'EOF'
variable "environment" { type = string }
output "name" { value = "dfd-${var.environment}" }
EOF
cat > main.tf <<'EOF'
module "name" {
source = "./modules/name"
environment = "dev"
}
output "name" { value = module.name.name }
EOF
terraform init
terraform console <<'EOF'
module.name.name
EOF
Checked against the linked documentation in October 2026. A later release can move a flag, a price, or a supported pair. Use the page for the version you installed.