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.
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)- B83% (15)
- C6% (1)
- D11% (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.
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.
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.
Variables are designed to accept external input into a module, not to store and reuse internal expressions computed within the same configuration.
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
Community Discussion
No community discussion yet for this question.