nerdexam
HashiCorp

TA-002-P · Question #177

Hanah is writing a terraform configuration with nested modules, there are multiple places where she has to use the same conditional expression but she wants to avoid repeating the same values or…

The correct answer is B. Local Values. Local values allow you to assign a name to an expression once and reference it throughout the module, eliminating repetition of complex or conditional expressions.

Read, generate, and modify configuration

Question

Hanah is writing a terraform configuration with nested modules, there are multiple places where she has to use the same conditional expression but she wants to avoid repeating the same values or expressions multiple times in the configuration,. What is a better approach to dealing with this?

Options

  • AExpressions
  • BLocal Values
  • CVariables
  • DFunctions

How the community answered

(18 responses)
  • B
    83% (15)
  • C
    6% (1)
  • D
    11% (2)

Why each option

Local values allow you to assign a name to an expression once and reference it throughout the module, eliminating repetition of complex or conditional expressions.

AExpressions

Expressions are the syntax constructs used to compute values inline; they are not a named storage mechanism and cannot be defined once and reused by name.

BLocal ValuesCorrect

A locals block lets Hanah define a conditional expression once under a chosen name and then reference it as local.<NAME> in every place it is needed, keeping the configuration DRY without accepting external input or defining reusable functions.

CVariables

Variables are designed to accept external input into a module, not to store and reuse internal expressions computed within the same configuration.

DFunctions

Functions are built-in Terraform operations that transform values; they cannot be defined by the user to encapsulate and reuse a specific conditional expression.

Concept tested: Terraform local values for avoiding repeated expressions

Source: https://developer.hashicorp.com/terraform/language/values/locals

Topics

#Local Values#Code Reusability#Terraform Language#Configuration Best Practices

Community Discussion

No community discussion yet for this question.

Full TA-002-P Practice