A nested dynamic block is a dynamic block placed inside another dynamic block, allowing you to generate complex, repeated nested configurations based on input data.
Click to reveal answer
beginner
Why use nested dynamic blocks instead of static blocks?
Nested dynamic blocks let you create flexible, reusable infrastructure code that adapts to varying input sizes and structures without manually writing repetitive blocks.
Click to reveal answer
beginner
In Terraform, what keyword is used to define a dynamic block?
The keyword dynamic is used to define a dynamic block, which generates nested blocks based on a for_each expression.
Click to reveal answer
intermediate
How do you access the inner block's content inside a nested dynamic block?
Inside a nested dynamic block, you use content to define the inner block's attributes, and you can reference the current item with each.value or each.key.
Click to reveal answer
intermediate
What is a common use case for nested dynamic blocks in Terraform?
A common use case is defining complex resources like security groups with multiple rules, where each rule has multiple nested attributes like ports and protocols.
Click to reveal answer
What does a nested dynamic block in Terraform allow you to do?
AGenerate repeated nested blocks based on input data
BCreate static blocks only
CRun Terraform commands automatically
DDefine variables outside modules
✗ Incorrect
Nested dynamic blocks generate repeated nested blocks dynamically based on input data.
Which keyword starts a dynamic block in Terraform?
Adynamic
Bblock
Crepeat
Dfor_each
✗ Incorrect
The keyword 'dynamic' is used to define dynamic blocks.
How do you reference the current item inside a dynamic block?
Aitem.value
Bcurrent.value
Ceach.value
Dthis.value
✗ Incorrect
Inside dynamic blocks, 'each.value' references the current item.
What is the purpose of the 'content' block inside a dynamic block?
AImports modules
BRuns Terraform apply
CDeclares variables
DDefines the nested block's attributes
✗ Incorrect
'content' defines the attributes of the nested block generated by the dynamic block.
Which scenario best fits using nested dynamic blocks?
ARunning Terraform plan
BCreating multiple nested firewall rules with varying ports
CDefining a single static resource
DSetting environment variables
✗ Incorrect
Nested dynamic blocks are ideal for complex nested configurations like multiple firewall rules.
Explain how nested dynamic blocks work in Terraform and why they are useful.
Think about how you can repeat blocks inside other repeated blocks.
You got /4 concepts.
Describe a real-world example where nested dynamic blocks would simplify Terraform code.
Consider network security settings with many detailed rules.
You got /4 concepts.
Practice
(1/5)
1.
What is the main purpose of using nested dynamic blocks in Terraform?
easy
A. To write static configuration without loops
B. To create repeated blocks inside other repeated blocks dynamically
C. To define provider settings only
D. To manage Terraform state files
Solution
Step 1: Understand dynamic blocks
Dynamic blocks allow repeating a block multiple times based on a list or map.
Step 2: Understand nested dynamic blocks
Nested dynamic blocks repeat blocks inside other repeated blocks, useful for complex nested lists.
Final Answer:
To create repeated blocks inside other repeated blocks dynamically -> Option B
C. Using var.subnets instead of network.value.subnets for nested for_each
D. network.value.name is invalid syntax
Solution
Step 1: Check nested for_each source
The nested dynamic block should iterate over subnets related to the current network, not the entire var.subnets list.
Step 2: Confirm correct variable usage
Using var.subnets ignores the current network context; it should be network.value.subnets to nest properly.
Final Answer:
Using var.subnets instead of network.value.subnets for nested for_each -> Option C
Quick Check:
Nested for_each must use parent block's value [OK]
Hint: Nested loops must use parent block's current item [OK]
Common Mistakes:
Using global variables instead of nested context
Forgetting quotes around dynamic block names
Misplacing content blocks
5.
You have a variable var.services which is a list of maps. Each map has a name and a list of ports. You want to create an aws_security_group resource with nested dynamic blocks to create ingress rules for each port of each service. Which Terraform code snippet correctly implements this?
hard
A.
resource "aws_security_group" "example" {
for_each = { for s in var.services : s.name => s }
name = each.key
dynamic "ingress" {
for_each = each.value.ports
content {
from_port = ingress.value
to_port = ingress.value
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
}