0
0
Terraformcloud~10 mins

Why dynamic blocks reduce repetition in Terraform - Visual Breakdown

Choose your learning style9 modes available
Process Flow - Why dynamic blocks reduce repetition
Start with repeated blocks
Identify repeated pattern
Create dynamic block with for_each
Terraform loops over items
Generates multiple blocks dynamically
Result: Less code, easier changes
End
Dynamic blocks let Terraform repeat similar blocks automatically, so you write less code and avoid copying the same structure many times.
Execution Sample
Terraform
resource "aws_security_group" "example" {
  name = "example"

  dynamic "ingress" {
    for_each = var.ingress_rules
    content {
      from_port   = ingress.value.from_port
      to_port     = ingress.value.to_port
      protocol    = ingress.value.protocol
      cidr_blocks = ingress.value.cidr_blocks
    }
  }
}
This Terraform code uses a dynamic block to create multiple ingress rules from a list, instead of writing each rule manually.
Process Table
StepActionInputOutputNotes
1Start resource definitionNo ingress rules yetResource block with dynamic ingress blockSetup for dynamic block processing
2Evaluate for_eachvar.ingress_rules list with 2 rulesLoop over 2 ingress rulesTerraform prepares to create 2 ingress blocks
3First iterationingress.value = first ruleCreate ingress block with first rule's ports and CIDRsFirst ingress block generated
4Second iterationingress.value = second ruleCreate ingress block with second rule's ports and CIDRsSecond ingress block generated
5Finish dynamic blockAll rules processedResource has 2 ingress blocksDynamic block replaced repetition
6Apply resourceResource with 2 ingress blocksSecurity group created with 2 ingress rulesDeployment reflects dynamic blocks
💡 All ingress rules processed, dynamic block replaced repeated code blocks
Status Tracker
VariableStartAfter 1After 2Final
ingress.valueundefined{from_port=80, to_port=80, protocol="tcp", cidr_blocks=["0.0.0.0/0"]}{from_port=443, to_port=443, protocol="tcp", cidr_blocks=["0.0.0.0/0"]}No more rules
Key Moments - 3 Insights
Why does Terraform loop over var.ingress_rules in the dynamic block?
Terraform uses the for_each expression to repeat the block for each item in var.ingress_rules, as shown in execution_table rows 2-4.
How does using a dynamic block reduce repetition compared to writing blocks manually?
Instead of writing each ingress block by hand, the dynamic block automatically creates them for each rule, reducing code duplication (see execution_table rows 3 and 4).
What happens if var.ingress_rules is empty?
The dynamic block creates zero ingress blocks, so no repeated blocks appear, avoiding empty or unused code sections.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is ingress.value during the second iteration?
A{from_port=80, to_port=80, protocol="tcp", cidr_blocks=["0.0.0.0/0"]}
B{from_port=443, to_port=443, protocol="tcp", cidr_blocks=["0.0.0.0/0"]}
Cundefined
DEmpty list
💡 Hint
Check execution_table row 4 under Input column
At which step does Terraform finish creating all ingress blocks?
AStep 5
BStep 4
CStep 3
DStep 6
💡 Hint
Look for 'Finish dynamic block' in execution_table
If var.ingress_rules had 3 rules instead of 2, how would execution_table change?
ATerraform would error out
BThe number of iterations stays 2
CThere would be 3 iterations creating ingress blocks
DOnly the first 2 rules are processed
💡 Hint
Refer to how for_each loops over all items in the list (execution_table rows 2-4)
Concept Snapshot
Terraform dynamic blocks let you write one block that repeats for each item in a list.
Use for_each inside dynamic to loop.
Terraform creates one block per item automatically.
This reduces manual repetition and makes code cleaner.
If the list is empty, no blocks are created.
Dynamic blocks improve maintainability and reduce errors.
Full Transcript
This visual execution shows how Terraform dynamic blocks reduce repetition by looping over a list of ingress rules. Initially, the resource has a dynamic block with for_each set to var.ingress_rules. Terraform evaluates the list and loops over each rule. For each iteration, it creates an ingress block with the rule's ports and CIDRs. After processing all rules, the resource ends up with multiple ingress blocks without manually writing each one. Variables like ingress.value change each iteration to the current rule. This approach avoids copying the same block multiple times and makes the code easier to maintain. If the list is empty, no ingress blocks are created, keeping the resource clean. The execution table traces each step from start to finish, showing how the dynamic block replaces repeated code with a loop.