TA-002-P · Question #31
Which of the following is the correct way to pass the value in the variable num_servers into a module with the input servers?
The correct answer is A. servers = num_servers. When passing the value of a variable to a module input, you typically refer to the variable by its name within the current scope.
Question
Which of the following is the correct way to pass the value in the variable num_servers into a module with the input servers?
Options
- Aservers = num_servers
- Bservers = variable.num_servers
- Cservers = var(num_servers)
- Dservers = var.num_servers
How the community answered
(13 responses)- A85% (11)
- B8% (1)
- C8% (1)
Why each option
When passing the value of a variable to a module input, you typically refer to the variable by its name within the current scope.
In Terraform, when passing a value to a module's input, if `num_servers` represents a local value, an output from another resource, or a directly accessible variable in the current scope, you refer to it simply by its name, `num_servers`.
`variable.num_servers` is an incorrect syntax for referencing a variable's value in this context.
`var(num_servers)` is an invalid and non-existent syntax in Terraform.
`var.num_servers` is the correct syntax for referencing an *input variable* that has been declared with a `variable "num_servers" {}` block within the same configuration, but if `num_servers` refers to a local or output value, direct naming is used.
Concept tested: Terraform module input variables and variable referencing
Source: https://developer.hashicorp.com/terraform/language/modules/syntax
Topics
Community Discussion
No community discussion yet for this question.