Which of the following correctly references a variable named num_instances in Terraform?

Prepare for the HashiCorp Terraform Associate Exam with quizzes, flashcards, and multiple-choice questions. Each question includes hints and explanations. Boost your confidence and ace your exam!

Multiple Choice

Which of the following correctly references a variable named num_instances in Terraform?

Explanation:
In Terraform, you access a declared variable using the var prefix. If a variable is named num_instances, you reference it as var.num_instances in your configuration. This tells Terraform to pull the value from the inputs you provide (through -var, a tfvars file, or a default in the variable block). The other forms point to different things and aren’t appropriate for referencing a variable: - num_instances by itself isn’t a valid variable reference; it would be treated as a plain token, which Terraform can’t resolve as a variable value. - local.num_instances would refer to a value defined in a locals block, not a variable. - data.num_instances would refer to a data source named num_instances, which requires a corresponding data block. Thus, var.num_instances is the correct way to reference the variable named num_instances. In modern Terraform, you can use it directly in expressions like count = var.num_instances without needing interpolation.

In Terraform, you access a declared variable using the var prefix. If a variable is named num_instances, you reference it as var.num_instances in your configuration. This tells Terraform to pull the value from the inputs you provide (through -var, a tfvars file, or a default in the variable block).

The other forms point to different things and aren’t appropriate for referencing a variable:

  • num_instances by itself isn’t a valid variable reference; it would be treated as a plain token, which Terraform can’t resolve as a variable value.

  • local.num_instances would refer to a value defined in a locals block, not a variable.

  • data.num_instances would refer to a data source named num_instances, which requires a corresponding data block.

Thus, var.num_instances is the correct way to reference the variable named num_instances. In modern Terraform, you can use it directly in expressions like count = var.num_instances without needing interpolation.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy