HashiCorp Terraform Associate (004) exam dumps

HashiCorp Terraform Associate (004) practice question 114 of 223

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

HashiCorp Terraform Associate (004) Question 114

Single answer4e Write dynamic configuration using expressions and functions

A platform team maintains a Terraform module that creates application subnets in multiple availability zones. The team wants each subnet name to include the environment and a zero-padded sequence number, such as dev-app-01, dev-app-02, and dev-app-03. They also want the module to automatically create one subnet per AZ listed in var.azs without hardcoding the count. Which configuration best meets these requirements using Terraform expressions and functions?

  1. A

    resource "aws_subnet" "app" { count = length(var.azs)

    availability_zone = var.azs[count.index] tags = { Name = format("%s-app-%02d", var.environment, count.index + 1) } }

  2. B

    resource "aws_subnet" "app" { count = var.azs

    availability_zone = element(length(var.azs), count.index) tags = { Name = "${var.environment}-app-${count.index}" } }

  3. C

    resource "aws_subnet" "app" { for_each = length(var.azs)

    availability_zone = each.value tags = { Name = upper(format("%s-app-%d", var.environment, each.key)) } }

  4. D

    resource "aws_subnet" "app" { count = length(var.azs) + 1

    availability_zone = element(var.azs, count.index) tags = { Name = format("%s-app-%02s", var.environment, count.index) } }

Show answer and explanation

Correct answer: A

Explanation

The best answer is the configuration that combines count, length, list indexing, and the format function correctly. In Terraform, count is commonly used when you need a resource instance for each item in a list and the instances can be addressed by numeric index. length(var.azs) dynamically determines how many subnets to create, and var.azs[count.index] selects the matching availability zone for each instance. The format function is the right tool for constructing predictable names, and %02d zero-pads integers to two digits. Because count.index begins at 0, adding 1 produces names like dev-app-01 instead of dev-app-00. This aligns with Terraform language documentation on meta-arguments, expressions, and built-in functions such as length and format.

  • A. Correct.

    Correct. This uses count = length(var.azs) to create one subnet per availability zone, indexes into the AZ list with var.azs[count.index], and uses the format function with %02d to produce zero-padded numbers such as 01, 02, and 03. Adding 1 to count.index ensures numbering starts at 1 instead of 0. This is a valid and practical use of Terraform expressions and functions for dynamic configuration.

  • B. Incorrect.

    Incorrect. count must be a whole number, but var.azs is expected to be a list of AZ names, so count = var.azs is invalid. The element function usage is also reversed: element expects a list first and an index second. In addition, the Name value does not zero-pad the sequence and would start at 0. This option reflects common mistakes with count and function argument order.

  • C. Incorrect.

    Incorrect. for_each cannot be assigned length(var.azs) because that produces a number, while for_each requires a map or a set of strings. Also, upper would convert the entire name to uppercase, which is not part of the requirement, and %d with each.key would not work as intended in this form because each.key would be a string if using a set, not a numeric sequence. This option misuses both for_each and formatting.

  • D. Incorrect.

    Incorrect. count = length(var.azs) + 1 would create one extra subnet beyond the number of availability zones. While element can accept a list and an index, this configuration would either wrap unexpectedly or otherwise produce incorrect AZ assignment behavior depending on the index. The format verb %02s is also inappropriate for generating a numeric zero-padded sequence from count.index in this scenario. It also starts numbering at 0 rather than 1.

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