HashiCorp Terraform Associate (004) exam dumps

HashiCorp Terraform Associate (004) practice question 113 of 223

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

HashiCorp Terraform Associate (004) Question 113

Single answer4e Write dynamic configuration using expressions and functions

A platform team wants to standardize tags across environments. They receive a variable environment with values such as dev, stage, or prod, and a variable common_tags of type map(string). They need every AWS resource to include all common tags plus a computed Name tag in the format <environment>-web-<two-digit index>, such as prod-web-01 and prod-web-02. The team is creating multiple EC2 instances with count and wants a Terraform expression that builds the tag map dynamically inside the resource block.

Which expression should they use for the tags argument to meet this requirement?

  1. A

    merge(var.common_tags, { Name = format("%s-web-%02d", var.environment, count.index + 1) })

  2. B

    concat(var.common_tags, { Name = "${var.environment}-web-${count.index}" })

  3. C

    { for k, v in var.common_tags : k => v if Name = format("%s-web-%02d", var.environment, count.index + 1) }

  4. D

    zipmap(keys(var.common_tags), values(var.common_tags), { Name = format("%s-web-%02d", var.environment, count.index + 1) })

Show answer and explanation

Correct answer: A

Explanation

The best solution is to use Terraform expressions and functions that match the data types involved. In this scenario, tags are a map, so merge is the correct function to combine the existing common_tags map with a newly computed Name tag. The format function is well suited for creating structured strings and supports zero-padded numeric formatting, which makes it ideal for generating names like prod-web-01. Because count.index starts at 0, adding 1 is necessary to match the requested numbering scheme.

This reflects common Terraform best practices: use merge for maps, concat for lists, and for expressions only when transforming collections is actually needed. HashiCorp documentation on functions and expressions covers merge, format, count.index, and collection handling, all of which are central to writing dynamic configuration for Terraform Associate 004.

  • A. Correct.

    Correct. merge is the appropriate function for combining maps in Terraform. var.common_tags is already a map(string), and the second argument creates another map containing the dynamically computed Name tag. format("%s-web-%02d", var.environment, count.index + 1) produces values like prod-web-01, with %02d zero-padding the number to two digits. Using count.index + 1 converts Terraform's zero-based index into the required one-based numbering.

  • B. Incorrect.

    Incorrect. concat is used for combining lists or tuples, not maps. Since tags are represented as a map in Terraform providers such as AWS, this expression would fail type checking. A common misconception is assuming concat can combine any collection type, but Terraform distinguishes between list and map operations.

  • C. Incorrect.

    Incorrect. This is not valid Terraform syntax for the stated purpose. A for expression over a map can transform or filter elements, but it cannot inject a separate Name attribute using an if clause this way. The if in a for expression is only for filtering items, not for defining additional keys. Someone might choose this option because for expressions are often used for dynamic transformations, but this does not construct the required merged tag map.

  • D. Incorrect.

    Incorrect. zipmap accepts exactly two arguments: a list of keys and a corresponding list of values, and returns a map. It is not used to append an extra map argument. Even if rewritten, it would be unnecessary here because the input is already a map and only needs one additional key. This distractor targets confusion between map construction functions and map merge operations.

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