HashiCorp Terraform Associate (004) exam dumps

HashiCorp Terraform Associate (004) practice question 115 of 223

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

HashiCorp Terraform Associate (004) Question 115

Single answer4e Write dynamic configuration using expressions and functions

A 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?

  1. 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) }) }

  2. 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}" }) }

  3. 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]) }) }

  4. 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. merge is the appropriate function for combining multiple maps into a single tags map. format("%s-web-%d", var.environment, count.index + 1) correctly builds the Name value and adjusts the zero-based count.index so the instance names start at 1, producing values like dev-web-1, dev-web-2, and dev-web-3.

  • B. Incorrect.

    Incorrect. concat is used to combine lists or tuples, not maps. Since tags expects a map of string keys and values, using concat with local.common_tags and another map is invalid. This distractor targets the common misconception that any collection types can be combined with the same function. It also uses count.index directly, which would start at 0 instead of 1.

  • C. Incorrect.

    Incorrect. Although merge is the right function for combining tag maps, join requires a list of strings. count.index + 1 is a number, so this expression is not valid unless explicitly converted to a string first, for example with tostring(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_tags with additional attributes, you must use a function such as merge. In addition, this option uses count.index without adding 1, so the naming would begin at 0, which does not meet the requirement.

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