HashiCorp Terraform Associate (004) Question 115
Single answer4e Write dynamic configuration using expressions and functionsA platform team wants to standardize tags across AWS resources in Terraform. They receive an input variable environment and need to generate a Name tag dynamically in the format <environment>-web-<index>, where the index starts at 1 for each EC2 instance. They also want to add a common set of tags from a local value. The team currently provisions instances with count = 3.
Which configuration correctly uses Terraform expressions and functions to produce the required tags for each instance?
- A
locals { common_tags = { Owner = "platform" App = "web" } }
resource "aws_instance" "web" { count = 3
tags = merge(local.common_tags, { Name = format("%s-web-%d", var.environment, count.index + 1) }) }
- B
locals { common_tags = { Owner = "platform" App = "web" } }
resource "aws_instance" "web" { count = 3
tags = concat(local.common_tags, { Name = "${var.environment}-web-${count.index}" }) }
- C
locals { common_tags = { Owner = "platform" App = "web" } }
resource "aws_instance" "web" { count = 3
tags = merge(local.common_tags, { Name = join("-", [var.environment, "web", count.index + 1]) }) }
- D
locals { common_tags = { Owner = "platform" App = "web" } }
resource "aws_instance" "web" { count = 3
tags = { local.common_tags Name = format("%s-web-%s", var.environment, count.index) } }
Show answer and explanation
Correct answer: A
Explanation
This question tests practical use of Terraform expressions and functions for dynamic configuration. In Terraform, count.index is zero-based, so when a naming convention requires numbering from 1, you must adjust it with count.index + 1. To combine reusable tags stored in a local value with resource-specific tags, merge is the correct function because tags are represented as maps. The format function is a clean and reliable way to build strings from mixed value types, such as strings and numbers, without needing explicit interpolation. By contrast, concat is only for list-like collections, and join requires a list of strings. These behaviors align with Terraform language documentation for functions, expressions, collection types, and the count meta-argument.
- A. Correct.
Correct.
mergeis the appropriate function for combining multiple maps into a single tags map.format("%s-web-%d", var.environment, count.index + 1)correctly builds theNamevalue and adjusts the zero-basedcount.indexso the instance names start at 1, producing values likedev-web-1,dev-web-2, anddev-web-3. - B. Incorrect.
Incorrect.
concatis used to combine lists or tuples, not maps. Sincetagsexpects a map of string keys and values, usingconcatwithlocal.common_tagsand another map is invalid. This distractor targets the common misconception that any collection types can be combined with the same function. It also usescount.indexdirectly, which would start at 0 instead of 1. - C. Incorrect.
Incorrect. Although
mergeis the right function for combining tag maps,joinrequires a list of strings.count.index + 1is a number, so this expression is not valid unless explicitly converted to a string first, for example withtostring(count.index + 1). This option tests understanding of Terraform type constraints in function arguments. - D. Incorrect.
Incorrect. Terraform does not support inserting one map directly inside another map literal in this way. To combine
local.common_tagswith additional attributes, you must use a function such asmerge. In addition, this option usescount.indexwithout adding 1, so the naming would begin at 0, which does not meet the requirement.