HashiCorp Terraform Associate (004) exam dumps

HashiCorp Terraform Associate (004) practice question 111 of 223

Terraform Associate 004. Associate level, HashiCorp. Free question with the correct answer and a full explanation.

HashiCorp Terraform Associate (004) Question 111

Single answer4e Write dynamic configuration using expressions and functions

A team manages application settings in Terraform using a map variable and wants to pass those settings to a resource as a list of "key=value" strings. They also want the list to be stable across runs so plans do not show unnecessary diffs when the input map content has not changed. Given the variable below, which expression should they use?

variable "app_settings" { type = map(string) default = { LOG_LEVEL = "info" REGION = "us-east-1" FEATURE_X = "enabled" } }

The target argument expects a list of strings such as ["FEATURE_X=enabled", "LOG_LEVEL=info", "REGION=us-east-1"].

  1. A

    [for k in sort(keys(var.app_settings)) : "${k}=${var.app_settings[k]}"]

  2. B

    tolist(var.app_settings)

  3. C

    values(var.app_settings)

  4. D

    [for k, v in var.app_settings : join("=", [k, v])]

Show answer and explanation

Correct answer: A

Explanation

The best answer is the expression that combines a for expression with keys() and sort() to build a deterministic list from a map. In Terraform, maps are collections addressed by key, and when you need a predictable list output, you should explicitly sort the keys before constructing the list. This is especially important when a resource argument expects a list, because list ordering can affect plan output and create unwanted diffs.

The correct pattern is: [for k in sort(keys(var.app_settings)) : "${k}=${var.app_settings[k]}"]

This uses core Terraform language features covered in the Associate exam domain for dynamic configuration: expressions, for expressions, collection functions, and string interpolation. HashiCorp documentation for Terraform expressions and functions describes using for expressions to transform complex values and using functions like keys(), values(), and sort() when working with collections. Explicit sorting is a practical best practice whenever you convert unordered data structures such as maps into ordered ones such as lists.

  • A. Correct.

    Correct. This expression first gets the map keys with keys(var.app_settings), sorts them with sort(...), and then uses a for expression to build each string in a deterministic order. Using var.app_settings[k] retrieves the value for each sorted key. This is a common pattern when converting a map into a stable list representation to avoid plan noise caused by unordered map iteration.

  • B. Incorrect.

    Incorrect. tolist() cannot convert a map directly into the required list of "key=value" strings. Even if a conversion were possible, it would not produce the desired structure because the target argument expects strings combining both keys and values.

  • C. Incorrect.

    Incorrect. values(var.app_settings) returns only the map values, such as ["enabled", "info", "us-east-1"], without the corresponding keys. That does not satisfy the requirement to produce "key=value" strings. Candidates may choose this because values() is a valid function for maps, but it loses necessary information.

  • D. Incorrect.

    Incorrect. This for expression does produce "key=value" strings, but iteration over a map should not be relied on for a stable custom ordering in list output. If the downstream argument is order-sensitive or Terraform compares list ordering, this can lead to unnecessary diffs. The better practice is to explicitly sort the keys first.

Timed practice exam

Take a HashiCorp Terraform Associate (004) practice test under exam conditions

70 questions in 60 minutes, drawn from this bank, with a score report and a per-question review when you finish.

Start timed exam