Terraform Associate 004 ยท Dynamic configuration
Local values: name an expression once, reuse it
locals block, reuse and readability, difference from variables and outputs (4e).
Key points
- A locals block assigns names to expressions, and you read them as local.name (singular).
- Locals can use variables, resource attributes, functions and other locals, and exist only inside their own module.
- Unlike input variables, callers cannot override them; unlike outputs, they are not exposed to the parent.
- Use them to avoid repeating expressions, not for single-use simple values.
Source: https://developer.hashicorp.com/terraform/language/values/locals
Practice questions
A locals block defines region = "eu-west-1". How do you reference it elsewhere in the module?
- locals.region
- var.region
- local.region
- local["region"].value
Show answer
Answer: C. local.region
The block is called locals, but references use the singular local. prefix: local.region. var. is for input variables.
Which statements about local values are correct?
- A local can reference variables, resource attributes and other locals
- A local is available only inside the module that defines it
- To use a local in a child module, pass it as an argument
- A local can be overridden with -var on the command line
- You read a local as locals.name
Show answer
Answer: A. A local can reference variables, resource attributes and other locals, B. A local is available only inside the module that defines it, C. To use a local in a child module, pass it as an argument
Locals can build on variables, resources and other locals; they are visible only in their own module and must be passed to a child module as an argument. They cannot be set with -var, and the reference prefix is local, not locals.
True or false: because the block is named locals, you reference its values as locals.name.
Show answer
Answer: False
False. The block keyword is plural but references use the singular local.name.
Practice 8 questions on this topic in Bitzu
Short lessons, instant explanations and spaced review of what you get wrong. Free on iOS and Android.
