nerdexam
HashiCorp

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.

Interact with Terraform modules

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)
  • A
    85% (11)
  • B
    8% (1)
  • C
    8% (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.

Aservers = num_serversCorrect

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`.

Bservers = variable.num_servers

`variable.num_servers` is an incorrect syntax for referencing a variable's value in this context.

Cservers = var(num_servers)

`var(num_servers)` is an invalid and non-existent syntax in Terraform.

Dservers = var.num_servers

`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

#Terraform modules#Module inputs#Variable assignment

Community Discussion

No community discussion yet for this question.

Full TA-002-P Practice