What are dynamic blocks in Terraform and when are they useful?
Learn how Terraform dynamic blocks generate repeated nested blocks from a collection, with syntax, a security group example, and when to avoid them.
Expected Interview Answer
A dynamic block generates repeated nested configuration blocks programmatically by iterating over a collection, letting you produce a variable number of inner blocks like ingress rules or settings from a list or map instead of hand-writing each one.
Dynamic blocks solve the problem that you cannot use count or for_each directly on nested blocks inside a resource. You declare dynamic with the block's name as its label, provide a for_each collection, and reference each element through the iterator (by default named after the block) inside a content block. They are ideal when the number of nested blocks is data-driven, but overusing them hurts readability, so prefer static blocks when the set is small and fixed.
- Generates a variable number of nested blocks from data
- Removes repetitive copy-pasted configuration
- Keeps modules flexible and DRY
- Drives configuration from variables, lists or maps
- Reduces errors from manually duplicating similar blocks
AI Mentor Explanation
A dynamic block is like a team sheet generator: instead of writing each of the eleven player rows by hand, you feed in the squad list and it stamps out one identically formatted row per player. Give it the list of ingress rules the way you'd give it a squad, and it produces one properly filled nested block for each entry automatically.
Step-by-Step Explanation
Step 1
Identify repeated nested blocks
Find a nested block like ingress or setting that you'd otherwise copy-paste many times.
Step 2
Declare the dynamic block
Write dynamic "ingress" with the nested block's name as the label.
Step 3
Provide for_each
Set for_each to the list or map that drives how many blocks are generated.
Step 4
Reference the iterator
Inside content, access each element with ingress.value (or a named iterator).
Step 5
Keep it readable
Use dynamic blocks only when the count is data-driven; keep small fixed sets static.
What Interviewer Expects
- Understanding you can't use count/for_each directly on nested blocks
- Correct syntax: dynamic label, for_each, and content block
- Knowing how to reference each element via the iterator value
- Awareness that overuse harms readability
- A realistic example like security group ingress rules
Common Mistakes
- Using dynamic blocks for small fixed sets, hurting readability
- Forgetting the content block wrapper
- Referencing the wrong iterator name instead of the default
- Trying to put count directly on a nested block
- Not handling empty collections, producing zero blocks unexpectedly
Best Answer (HR Friendly)
“A dynamic block lets Terraform build repeated pieces of configuration automatically from a list, instead of copying and pasting them. It's useful when the number of items — like firewall rules — depends on data and can change.”
Code Example
variable "ingress_rules" {
type = list(object({
from_port = number
to_port = number
cidr = string
}))
}
resource "aws_security_group" "web" {
name = "web-sg"
dynamic "ingress" {
for_each = var.ingress_rules
content {
from_port = ingress.value.from_port
to_port = ingress.value.to_port
protocol = "tcp"
cidr_blocks = [ingress.value.cidr]
}
}
}Follow-up Questions
- Why can't you use count directly on a nested block?
- How do you rename the iterator in a dynamic block?
- When should you avoid dynamic blocks for readability?
- How do dynamic blocks behave with an empty for_each?
- Can you nest dynamic blocks inside one another?
MCQ Practice
1. What problem do dynamic blocks solve?
Dynamic blocks iterate a collection to produce a variable number of nested configuration blocks.
2. Inside a dynamic "ingress" block, how do you reference each element by default?
The default iterator is named after the block, so elements are accessed via ingress.value.
3. When are dynamic blocks a poor choice?
For a small fixed set, static nested blocks are clearer; dynamic blocks add unnecessary indirection.
Flash Cards
What is a dynamic block? — A construct that generates repeated nested blocks by iterating over a collection.
Why not just use count? — count and for_each can't be applied directly to nested blocks, only to resources and modules.
Default iterator name — The block's own label, e.g. ingress.value inside dynamic "ingress".
When to avoid them — When the nested set is small and fixed — static blocks are more readable.