Lesson 2 of 4 Terraform Modules

Module Inputs, Outputs and Variable Scope

Modules are isolated from each other: values pass in through variables and out through outputs. This lesson explains the scope rules and how values travel through a module tree.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain the scope of variables within modules.
  • Describe how values pass into and out of modules.

Each module has its own scope

Every module has its own namespace. Expressions in a module can refer only to that module's variables, locals, resources, data sources and the outputs of its own child modules. A child module cannot read its parent's var. values or locals, and a parent cannot read a child's variables, locals or resources. This isolation is what makes modules reusable: a module behaves the same wherever it is called, because it depends only on its inputs.

True or false

Module scope

Exam skills 5.1.1 · 5.2.1 · 5.2.2

Check what each module can see.

Log in to try this activity Interactive activities are free with an account.

Passing values in

Each variable block in a child module becomes an argument of the module block that calls it. Variables with defaults are optional arguments; variables without defaults are required.

# modules/web/variables.tf
variable "instance_type" {
  type    = string
  default = "t3.micro"
}

variable "subnet_id" {
  type = string
}

# Root module main.tf
module "web" {
  source        = "./modules/web"
  subnet_id     = module.network.public_subnet_id
  instance_type = var.web_instance_type
}

The root module's own variables are set from the command line, .tfvars files or TF_VAR_ environment variables, and the root passes them on explicitly. Only root module variables can be set from outside Terraform; TF_VAR_instance_type does not reach a child module's instance_type variable unless the root passes it.

The following diagram shows each level declaring and forwarding a variable.

Passing a value two module levels down. Each module sees only its own variables, so database cannot read the root's var.db_instance_class. The app and database modules each declare the variable, and each level passes it to the next through a module argument.
The module names are illustrative.
Put in order

Passing a value down two levels

Exam skills 5.2.1

Arrange the steps that pass a root variable into a module called by another module.

Log in to try this activity Interactive activities are free with an account.

Passing values out

A child exposes values with output blocks, and the parent reads them as module.NAME.OUTPUT. In the example, module.network.public_subnet_id requires the network module to declare output "public_subnet_id". Resources inside the module have addresses such as module.network.aws_subnet.public in state and commands, but expressions in the parent cannot refer to them directly.

  1. Root variable

    var.web_instance_type is set in terraform.tfvars.

  2. Module argument

    The root passes it as instance_type = var.web_instance_type.

  3. Child variable

    Inside the module it is var.instance_type.

  4. Child output

    The module returns output "public_ip" { value = aws_instance.this.public_ip }.

  5. Root reference

    The root reads module.web.public_ip and can re-export it as a root output.

Only root module outputs are printed after apply and available to terraform output. To see a child output, add a root output that returns it.

Worked example

A value two levels down

The root module calls an app module, which calls a database module. The root has a variable db_instance_class. A developer adds var.db_instance_class inside the database module and gets an error that the variable is not declared.

The decisive rule is module isolation.

The database module can see only its own variables. The developer must declare variable "db_instance_class" in the database module, declare one in the app module as well, pass db_instance_class = var.db_instance_class from the root to the app module, and pass it again from the app module to the database module. Each level declares and forwards the value.

Check yourself

Can a child module read var.region from its parent module?

Show answer

No. Each module has its own scope; the parent must pass the value through a module argument.

Exam Tips

Remember for the exam

  • A module sees only its own variables, locals and resources.
  • Variables become module block arguments; no default means required.
  • Outputs are the only way out: module.NAME.OUTPUT.
  • Only root variables can be set from CLI, files or TF_VAR_.