HashiCorp Terraform Associate (004) Question 113
Single answer4e Write dynamic configuration using expressions and functionsA 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?
- A
merge(var.common_tags, { Name = format("%s-web-%02d", var.environment, count.index + 1) })
- B
concat(var.common_tags, { Name = "${var.environment}-web-${count.index}" })
- C
{ for k, v in var.common_tags : k => v if Name = format("%s-web-%02d", var.environment, count.index + 1) }
- 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.
mergeis the appropriate function for combining maps in Terraform.var.common_tagsis already amap(string), and the second argument creates another map containing the dynamically computedNametag.format("%s-web-%02d", var.environment, count.index + 1)produces values likeprod-web-01, with%02dzero-padding the number to two digits. Usingcount.index + 1converts Terraform's zero-based index into the required one-based numbering. - B. Incorrect.
Incorrect.
concatis 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 assumingconcatcan 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
forexpression over a map can transform or filter elements, but it cannot inject a separateNameattribute using anifclause this way. Theifin aforexpression is only for filtering items, not for defining additional keys. Someone might choose this option becauseforexpressions are often used for dynamic transformations, but this does not construct the required merged tag map. - D. Incorrect.
Incorrect.
zipmapaccepts 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.