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 questionsIn 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.
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.
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.
Keep practicing
Take the free Terraform-Associate-004 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other Terraform-Associate-004 practice questions
- Question 1You should run terraform fnt to rewrite all Terraform configurations within the current w…
- Question 2Module variable assignments are inherited from the parent module and you do not need to e…
- Question 3terraform validate confirms thesyntaxof Terraform files.
- Question 4Using the terraform state rm command against a resource will destroy it.
- Question 5Variables declared within a module are accessible outside of the module.
- Question 6terraform init retrieves and caches the configuration for all remote modules.
