Dynamic nested blocks
One policy, several child blocks
Suppose an office application has two documented access rules. In this chapter, training_policy is a fictional provider resource, not a downloadable plugin or a real firewall. Its teaching schema requires one to three rule blocks. Read the following static configuration as a diagram expressed in HCL.
resource "training_policy" "office" { name = "office" rule { name = "staff" port = 443 } rule { name = "health" port = 8443 }}There is one parent resource and two child blocks. Each rule has a name and a port. A child block is part of the parent configuration, not an independently managed Terraform resource. There is no separate managed resource address for staff or health. For a fixed pair of rules, writing them explicitly is clear.
Generate the same blocks from a list
When the number of rules depends on input, repeat the block body rather than copy it. This separate illustrative configuration generates the same two rule blocks.
locals { rules = [ { name = "staff", port = 443 }, { name = "health", port = 8443 } ]}resource "training_policy" "office" { name = "office" dynamic "rule" { for_each = local.rules iterator = entry content { name = entry.value.name port = entry.value.port } }}dynamic "rule" identifies the child block kind. The collection supplies two entries; content describes the fields of each generated rule. iterator = entry introduces a temporary name for the current entry. The first entry supplies staff and 443; the second supplies health and 8443. Without the custom iterator, use rule.value.name instead. For list inputs, the iterator key is the position; for maps it is the map key. In a set, key and value coincide. This is not the resource-level each object.
Filter the collection before generating blocks
Assume var.rules is a list of objects with name, port and enabled fields. A disabled entry should not be emitted. This partial snippet belongs inside the fictional resource.
dynamic "rule" { for_each = [for r in var.rules : r if r.enabled] content { name = rule.value.name port = rule.value.port }}For staff enabled and health disabled, the expected expansion contains only staff. If both are disabled, zero blocks are generated, which violates this particular teaching schema. A dynamic block is not a way to bypass a minimum count. The worksheet asks you to predict the zero, one and two rule cases. When omission changes access or auditing, make that choice explicit and review it.
Choose a distinct iterator at each level
Some schemas define groups containing their own child rules. The following is a separate fictional schema extension, not supported by the basic office schema. Assume groups is a map of lists of rule objects.
dynamic "group" { for_each = var.groups iterator = g content { name = g.key dynamic "rule" { for_each = g.value iterator = r content { name = r.value.name port = r.value.port } } }}The outer name comes from the map key; the inner fields come from the current rule. For one group called internal with two entries, expect one group containing two rules, not two group resources. A second empty group would generate its group block with zero inner rules; its validity depends on the extended schema. Avoid deeply nested generation when a flatter interface would communicate the intent better.
Know when labels and dynamic generation are allowed
A block label is the name after the block type, not an ordinary attribute. Some block schemas permit labels. This separate hypothetical labeled-block example assumes named_rule accepts exactly one label and var.ports is a map of numbers.
dynamic "named_rule" { for_each = var.ports labels = [named_rule.key] content { port = named_rule.value }}For the map {staff = 443}, the expected text is a named_rule "staff" block containing port 443. Ordinary rule blocks in our teaching schema have no labels; adding labels there is invalid. The parent schema determines which blocks exist. You cannot use dynamic to invent a block type, emit top-level resources, or generate early-processed lifecycle or provisioner meta-argument blocks. For separate resource instances use resource-level count or for_each from earlier chapters. All examples here are static and unexecuted.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Dynamic nested blocks — knowledge check
Check nested configuration, schema interpretation and readable design.
What does dynamic "rule" generate in the office example?
What does entry.value.port read?
How many rule blocks does an empty input generate?
Which block cannot be generated with dynamic?
When can labels be supplied?