Bitzu

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

Single choice

A locals block defines region = "eu-west-1". How do you reference it elsewhere in the module?

  1. locals.region
  2. var.region
  3. local.region
  4. 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.

Select all that apply

Which statements about local values are correct?

  1. A local can reference variables, resource attributes and other locals
  2. A local is available only inside the module that defines it
  3. To use a local in a child module, pass it as an argument
  4. A local can be overridden with -var on the command line
  5. 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

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.

Download on the App StoreGet it on Google Play

More Terraform Associate 004 topics in this module