TF-Associate-004 - Terraform modules - Section 5c

Use modules in configuration.

A module block calls a module, passes inputs, and its outputs are referenced as module.name.output. Candidates should know count and for_each apply to module blocks, and how a called module's resources are addressed in state.

module blockpassing input valuesmodule output referencecount and for_each on modules

Practice question for this objective

Free sampleTerraform moduleseasy

A practitioner defines a child module named 'network' and wants the root configuration to reference the ID of a VPC that the module creates. The module already declares an output called 'vpc_id'. Which expression should the root configuration use to read that value?

module "network" {
  source = "./modules/network"
  cidr   = "10.0.0.0/16"
}
  • Amodule.network.outputs.vpc_id
  • Bvar.network.vpc_id
  • Cnetwork.vpc_id
  • Dmodule.network.vpc_id Correct
Reference a child module's exported output from the calling configuration using the module.NAME.OUTPUT syntax. Outputs declared inside a child module are exposed to the caller under the address module.<module name>.<output name>; there is no intermediate 'outputs' component and the module keyword is required to distinguish it from resource references.

Why A is wrong: This looks plausible because outputs are what is being read, but Terraform does not insert an 'outputs' segment into the reference address, so it fails to resolve.

Why B is wrong: The var prefix reads input variables of the current module, not values exported by a child module, so it cannot reach the module's output.

Why C is wrong: Omitting the module keyword makes this look like a resource reference, but a bare name without the module prefix does not address a called module's output.

Why D is correct: A child module's exported output is read with the syntax module.<NAME>.<OUTPUT>, so module.network.vpc_id returns the value the module exposed.

See more TF-Associate-004 practice questions, answers explained.

More in this domain

Back to all Terraform modules objectives, or the TF-Associate-004 cert hub.

Examworthy is not affiliated with or endorsed by HashiCorp. Original, blueprint-aligned practice material only.