A dynamic block lets you generate multiple nested blocks inside a resource or module based on a list or map, making configuration flexible and reusable.
Click to reveal answer
beginner
Which keyword starts a dynamic block in Terraform?
The keyword dynamic starts a dynamic block, followed by the block type in quotes, like dynamic "ingress".
Click to reveal answer
intermediate
What are the two main parts inside a dynamic block?
Inside a dynamic block, you have for_each to loop over a collection, and content which defines the block structure to repeat.
Click to reveal answer
intermediate
Why use dynamic blocks instead of static blocks?
Dynamic blocks reduce repetition by generating multiple blocks from data, making configurations easier to maintain and adapt to changing inputs.
Click to reveal answer
advanced
Show a simple example of a dynamic block creating multiple ingress rules.
A. The content block should use braces, not an assignment
B. The variable egress is undefined inside the block
C. Missing quotes around "egress" in dynamic block declaration
D. for_each cannot be used with variables
Solution
Step 1: Check dynamic block content syntax
The content must be a block, not an assignment. It should be content { ... }, not content = { ... }.
Step 2: Verify other parts
Variable egress is valid as the iterator, quotes are present, and for_each can use variables.
Final Answer:
The content block should use braces, not an assignment -> Option A
Quick Check:
content must be a block, not an equals assignment [OK]
Hint: Use content { ... } block, not content = { ... } [OK]
Common Mistakes:
Writing content as an assignment instead of a block
Assuming iterator variable is undefined
Forgetting quotes around dynamic block name
5. You want to create multiple network_interface blocks dynamically in an AWS instance resource using a list of maps with keys device_index and subnet_id. Which dynamic block correctly achieves this?