Bitzu

Terraform Associate 004 ยท Terraform modules

How does scope work between a root and a child module?

Module isolation, root vs child module, inputs as module variables, outputs as the only way back, no access to parent locals (5b).

Key points

  • Each module has its own namespace.
  • A child sees only the values the caller passes as arguments, which land in its input variables.
  • It cannot read the parent's variables or locals.
  • The caller sees only the child's output values, via module.NAME.OUTPUT, never its resources or locals directly.

Source: https://developer.hashicorp.com/terraform/language/modules/develop/structure

Practice questions

Single choice

How does a root module obtain a value computed inside a child module?

  1. Through an output value declared in the child module
  2. By reading the child's locals directly
  3. By reading the child's .tfstate file
  4. Child values are automatically visible as top-level variables
Show answer

Answer: A. Through an output value declared in the child module

Only declared outputs cross the module boundary outward. Child locals and resources are not addressable from the parent.

Select all that apply

Which statements about module scope are true?

  1. A child module only sees values passed as module block arguments
  2. A parent cannot reference a resource inside a child except through an output
  3. A child variable without a default must be set by the caller
  4. A child can read the parent's local values by name
  5. Sibling modules can read each other's variables directly
Show answer

Answer: A. A child module only sees values passed as module block arguments, B. A parent cannot reference a resource inside a child except through an output, C. A child variable without a default must be set by the caller

A child gets only what is passed in, required variables need a value, and parents cannot reach child resources. Child modules cannot read parent locals.

True or false

True or false: a child module can read the root module's local values directly.

Show answer

Answer: False

False. Values must be passed into the child as arguments that map to its input variables.

Practice 8 questions on this topic in Bitzu

Short lessons, instant explanations and spaced review of what you get wrong. Free on iOS and Android.

Download on the App StoreGet it on Google Play

More Terraform Associate 004 topics in this module