Terraform-Associate-004 practice question 14 of 35

Your root module contains a variable namednum_servers. Which is the correct way to pass its value to a child module with an input namedservers?

Full set: 360 questions
Answer options
A: servers = num_servers
B: servers = var(num_servers)
C: servers = var.num_servers Correct answer
D: servers = ${var.num_servers}
Correct Answer:
C. servers = var.num_servers
Explanation

In HashiCorp Configuration Language (HCL), when a root module calls a child module, it passes values to the child's input variables. The syntax for this assignment within the module block is = . To reference an input variable declared in the current (root) module, the correct syntax is var.. Therefore, to assign the value of the root module's numservers variable to the child module's servers input variable, the correct expression is servers = var.numservers.

Why Incorrect

A. servers = numservers is incorrect because it lacks the var. prefix required to reference an input variable's value. Terraform would interpret numservers as an undefined object.
B. servers = var(numservers) is incorrect. var() is not a valid function or syntax in HCL for accessing input variables.
D. servers = ${var.numservers} uses string interpolation syntax. While this was common in Terraform versions before 0.12, it is no longer necessary for direct assignments and is considered a legacy style.

References

1. HashiCorp Terraform Documentation, "Calling a Child Module": This page explicitly demonstrates the correct syntax for passing a root module's variable to a child module's input. The example shows instancetype = var.instancetype, which directly corresponds to the correct answer's format.

Source: HashiCorp, Terraform Documentation, Language -> Modules -> Calling a Child Module.

2. HashiCorp Terraform Documentation, "Input Variables": This section explains how to declare and reference input variables. Under the "Using Input Variable Values" subsection, it states, "Within the module that declared a variable, its value can be accessed using the expression var.."

Source: HashiCorp, Terraform Documentation, Language -> Input Variables -> Using Input Variable Values.

3. HashiCorp Terraform Documentation, "Expressions": This page details the syntax for expressions in HCL. It confirms that for top-level arguments, interpolation (${...}) is not needed and direct expressions like var.numservers are the correct, modern syntax.

Source: HashiCorp, Terraform Documentation, Language -> Expressions -> Syntax and Behavior.

Scroll to Top