Terraform-Associate-004 practice question 15 of 35

You want to define multiple data disks as nested blocks inside the resource block for a virtual machine. What Terraform feature would help you define the blocks using the values in a variable?

Full set: 360 questions
Answer options
A: Local values
B: Count arguments
C: Collection functions
D: Dynamic blocks Correct answer
Correct Answer:
D. Dynamic blocks
Explanation

Dynamic blocks are a specific Terraform feature designed to create multiple nested configuration blocks within a single resource definition. They iterate over a complex variable, such as a list of maps, and generate a block for each item in the collection. This directly addresses the requirement of defining multiple data disks (which are nested blocks) for a virtual machine based on the values in a variable. This approach avoids repetitive code and allows the configuration to be flexible and data-driven.

Why Incorrect

A. Local values: Local values are used to assign a name to an expression, creating a reusable variable within a module. They do not generate configuration blocks.
B. Count arguments: The count meta-argument creates multiple independent instances of a resource, not multiple nested blocks within a single resource.
C. Collection functions: Collection functions (e.g., merge, flatten) are used to manipulate and transform data structures like lists and maps, but they do not create configuration blocks themselves.

References

1. HashiCorp Terraform Documentation, "Dynamic Blocks": "You can dynamically construct repeatable nested blocks, such as setting for an azurermappservice resource, using a special dynamic block type... A dynamic block acts like a for expression, but produces nested blocks instead of a complex typed value." This page provides a direct explanation and examples of using dynamic blocks for the exact purpose described in the question.

2. HashiCorp Terraform Documentation, "The count Meta-Argument": "The count meta-argument... creates several instances of a resource or module." This clarifies that count operates at the resource level, not on nested blocks within a resource.

3. HashiCorp Terraform Documentation, "Local Values": "A local value assigns a name to an expression, allowing it to be used multiple times within a module without repeating it." This defines the purpose of locals as reusable expressions, not block generators.

Scroll to Top