Cert EspressoLearn. Practise. Find your flow.

A LITTLE FOCUS.
A BIG NEXT STEP.

Your pace. Your space.
HashiCorp · Terraform 004

Using modules and variable scope

Terraform 004 · Lesson 13 of 18 · 1:33:37

YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗

Learning with these videos? Subscribe for new courses and exam-style questions.

In this lesson

Prepare for the HashiCorp Terraform Associate (004) exam. In this lesson: calling a module with its inputs, variable scope between root and child modules, module outputs, and the standard structure for writing a module. Includes three original practice questions, with Bluefin Logistics, a fictional parcel delivery company, as the example.

Study notes

The text of this lesson, to read at your own pace. Information as of November 2026.

Values in and out

Bluefin's tracking configuration now calls the shared network module. But the module cannot see the region variable of the configuration, and the configuration cannot read the subnets inside the module. This lesson explains how values go in and out of a module, and how to write one.

Calling a module

In a module block, every argument other than source, version, and the meta-arguments sets an input variable of the child module. An input variable without a default value is required, so the module block must set it, or Terraform reports an error. Module blocks accept the meta-arguments count, for each, and depends on, plus providers, which passes provider configurations to the module. After you add a module block, run terraform init, so that Terraform installs the module. Each instance of the module then gets its own address, like module dot network, or module dot API with a key.

Variable scope

Each module has its own scope. A child module does not see the variables, locals, or resources of its parent, even when they have the same names. Values go in only through the input variables, the arguments of the module block, so the parent passes its own values explicitly, like region equals var dot region. This makes a module predictable: everything that it uses comes from its inputs, its own code, and its providers.

Module outputs

In the other direction, the parent cannot see the resources inside a child module. A module returns only what its output blocks declare, and the parent reads these module outputs as module dot name dot output, here module dot network dot subnet IDs. That is how modules are composed: the outputs of one module become the inputs of another, like the subnets of the network module passed to the API module. Keep the module tree flat when you can, with the root module wiring small modules together.

Writing a module

A reusable module usually has a main file for its resources, a variables file for its inputs, and an outputs file for what it returns. A read me file explains what the module does and how to call it, and a license file tells others how they may reuse it. Nested modules go in a modules directory inside the module. Give every input a description and a type, and every output a description, so that callers know what to pass. And when you move existing resources into a new module, a moved block keeps them in the state under their new address, without recreating them.

Recap

Let's recap. The arguments of a module block set the input variables of the child module, and the ones without a default are required. A child module never sees the variables, locals, or resources of its parent, so values go in through inputs. The parent reads only the outputs of a child, as module dot name dot output, and the outputs of one module feed the inputs of another. And a reusable module has main, variables, and outputs files, a read me file, and a moved block when resources move into it.

Quick checks for this lesson

The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.

  1. The network module declares variable "cidr_block" with no default. A module block calls it and sets only its source. What happens at terraform plan?

    • Terraform prompts for the missing value
    • The module uses an empty string instead
    • It takes the root's variable of the same name
    • An error: a required argument is missing
    Show the answer

    An error: a required argument is missing

    A variable without a default is a required argument of the module block, so Terraform reports an error. “Terraform prompts for the missing value” is wrong, because Terraform prompts only for the variables of the root module. “The module uses an empty string instead” is wrong, because Terraform never invents a value. And “It takes the root's variable of the same name” is wrong, because a child module never sees the variables of its parent, which the next part explains.

  2. The API module needs the ID of a security group that the network module creates, and the root module calls both. How should the API module get that ID?

    • Output it from network, pass it as an input
    • Refer to the group's resource address directly
    • Read a local value of the network module
    • Set a global variable that all modules see
    Show the answer

    Output it from network, pass it as an input

    The network module returns the ID through an output, and the root module passes that output to an input variable of the API module. “Refer to the group's resource address directly” is wrong, because the resources inside a module are out of reach from outside it. “Read a local value of the network module” is wrong, because locals stay inside their module. And “Set a global variable that all modules see” is wrong, because Terraform has no global variables: each module gets only its own inputs.

  3. Bluefin moves its existing bucket resource from the root module into a new storage module, and adds nothing else. What does the next plan propose for the bucket?

    • Nothing, since the bucket name is unchanged
    • An error: modules cannot own buckets
    • Destroy the bucket and create it again
    • Import the bucket into the module
    Show the answer

    Destroy the bucket and create it again

    The state still has the bucket under its old address, and the configuration now has it under a new one, so the plan destroys one and creates the other. “Nothing, since the bucket name is unchanged” is wrong, because Terraform tracks objects by their address, not by the name of the cloud resource. “An error: modules cannot own buckets” is wrong, because a module can manage any resource. And “Import the bucket into the module” is wrong, because Terraform never imports on its own: a moved block, which lesson 15 covers in detail, is the fix.