Nested dynamic blocks in Terraform - Commands & Configuration
variable "firewalls" { type = list(object({ name = string rules = list(object({ protocol = string ports = list(number) })) })) default = [ { name = "fw1" rules = [ { protocol = "tcp", ports = [80, 443] }, { protocol = "udp", ports = [53] } ] }, { name = "fw2" rules = [ { protocol = "tcp", ports = [22] } ] } ] } resource "example_firewall" "firewall" { for_each = { for fw in var.firewalls : fw.name => fw } name = each.key dynamic "rule" { for_each = each.value.rules content { protocol = rule.value.protocol dynamic "port" { for_each = rule.value.ports content { number = port.value } } } } }
This Terraform file defines a variable firewalls which is a list of firewall objects. Each firewall has a name and a list of rules. Each rule has a protocol and a list of ports.
The example_firewall resource uses for_each to create one resource per firewall.
Inside the resource, a dynamic "rule" block creates multiple rule blocks based on the rules list.
Inside each rule block, another dynamic "port" block creates multiple port blocks for each port number.
This nested dynamic block structure lets Terraform generate complex nested configurations automatically.
terraform init
terraform plan
terraform apply -auto-approve
-auto-approve - Skip interactive approval promptterraform show
If you remember nothing else from this pattern, remember: nested dynamic blocks let you generate multiple layers of repeated configuration automatically from complex input data.
Practice
What is the main purpose of using nested dynamic blocks in Terraform?
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 BQuick Check:
Nested dynamic blocks = repeated blocks inside repeated blocks [OK]
- Confusing dynamic blocks with static blocks
- Using nested dynamic blocks for simple flat lists
- Thinking dynamic blocks manage state files
Which of the following is the correct syntax to start a nested dynamic block named subnet inside a dynamic block named network?
dynamic "network" {
for_each = var.networks
content {
name = network.value.name
// What goes here?
}
}Solution
Step 1: Identify correct dynamic block syntax
Dynamic blocks require the keyword dynamic, block name in quotes, then for_each and content blocks.Step 2: Check nested dynamic block context
Inside the network dynamic block, use network.value.subnets for for_each and define content with subnet.value.Final Answer:
dynamic "subnet" { for_each = network.value.subnets content { cidr_block = subnet.value } } -> Option AQuick Check:
Correct syntax uses quotes and nested for_each [OK]
- Omitting quotes around block name
- Using wrong variable for for_each
- Placing for_each outside the braces
Given the following Terraform snippet, what will be the value of resource.aws_vpc.example tags after deployment?
variable "vpcs" {
default = [
{ name = "vpc1", tags = ["tag1", "tag2"] },
{ name = "vpc2", tags = ["tag3"] }
]
}
resource "aws_vpc" "example" {
for_each = { for v in var.vpcs : v.name => v }
cidr_block = "10.0.0.0/16"
dynamic "tags" {
for_each = each.value.tags
content {
key = tags.value
value = "true"
}
}
}Solution
Step 1: Understand for_each on resource
Resource creates two VPCs named vpc1 and vpc2 with their respective tags list.Step 2: Analyze nested dynamic block for tags
Dynamic block iterates over each.value.tags, creating a tag key with value "true" for each tag string.Final Answer:
{"tag1" = "true", "tag2" = "true"} for vpc1 and {"tag3" = "true"} for vpc2 -> Option DQuick Check:
Dynamic tags create key-value pairs from list items [OK]
- Assuming tags remain as list instead of key-value map
- Thinking dynamic blocks cannot be nested
- Ignoring the for_each on resource
Identify the error in this nested dynamic block snippet:
dynamic "network" {
for_each = var.networks
content {
name = network.value.name
dynamic "subnet" {
for_each = var.subnets
content {
cidr_block = subnet.value.cidr
}
}
}
}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 CQuick Check:
Nested for_each must use parent block's value [OK]
- Using global variables instead of nested context
- Forgetting quotes around dynamic block names
- Misplacing content blocks
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?
Solution
Step 1: Use for_each on resource with map keyed by service name
This allows referencing each service by name with each.key and each.value.Step 2: Use nested dynamic block for ingress rules
Dynamic block named "ingress" iterates over each.value.ports with for_each and uses ingress.value for port numbers.Final Answer:
Code snippet in Option A correctly uses nested dynamic blocks with proper variable references -> Option AQuick Check:
Nested dynamic blocks need correct for_each and value references [OK]
- Omitting quotes around dynamic block name
- Using wrong variable names like ports.value instead of ingress.value
- Not using map for resource for_each
Ready to prove it?
Pass the test and this pattern moves unknown
