0
0
Terraformcloud~10 mins

Dynamic blocks vs for_each decision in Terraform - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Process Flow - Dynamic blocks vs for_each decision
Start
Identify repeated config
Is block structure repeated?
NoUse for_each on resource
Yes
Use dynamic block inside resource
Generate repeated nested blocks
Apply configuration
Decide if repeated nested blocks need dynamic blocks or if repeated resources use for_each, then apply configuration accordingly.
Execution Sample
Terraform
resource "aws_security_group" "example" {
  name = "example"

  dynamic "ingress" {
    for_each = var.ingress_rules
    content {
      from_port = ingress.value.from
    }
  }
}
This code uses a dynamic block to create multiple ingress rules inside one security group resource.
Process Table
StepActionInputResultNotes
1Start processing resourceaws_security_group exampleResource block beginsPrepare to configure security group
2Evaluate for_each in dynamic blockvar.ingress_rules = [{from=80}, {from=443}]Two ingress blocks to generateDynamic block will loop twice
3Generate first ingress blockingress.value.from = 80Ingress block with from_port=80 createdFirst nested block created
4Generate second ingress blockingress.value.from = 443Ingress block with from_port=443 createdSecond nested block created
5Finish resource configurationAll blocks generatedResource ready for deploymentDynamic blocks replaced repeated nested blocks
6Apply terraform planResource with 2 ingress blocksSecurity group created with 2 ingress rulesDeployment successful
💡 All dynamic blocks generated and resource configured, ready for deployment
Status Tracker
VariableStartAfter 1After 2Final
var.ingress_rules[{from=80}, {from=443}][{from=80}, {from=443}][{from=80}, {from=443}][{from=80}, {from=443}]
ingress.value.fromN/A80443N/A after loop
Key Moments - 2 Insights
Why use dynamic blocks instead of for_each on the resource?
Dynamic blocks are used when you need to repeat nested blocks inside a single resource, as shown in steps 2-4 of the execution_table. For_each on the resource creates multiple separate resources instead.
Can for_each replace dynamic blocks for nested repeated blocks?
No, for_each on the resource creates multiple resources, not multiple nested blocks inside one resource. Dynamic blocks handle repeated nested blocks inside a single resource (see step 3 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of ingress.value.from at Step 4?
A80
B443
CN/A
D0
💡 Hint
Check the 'Input' column at Step 4 in execution_table
At which step does Terraform finish generating all dynamic blocks?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Look for 'Finish resource configuration' in the 'Action' column
If var.ingress_rules had 3 items instead of 2, how would the execution_table change?
AThere would be 3 rows generating ingress blocks instead of 2
BThe resource would be duplicated 3 times
CThe dynamic block would not run
DNo change in execution_table
💡 Hint
Refer to how dynamic blocks loop over var.ingress_rules in steps 2-4
Concept Snapshot
Dynamic blocks repeat nested blocks inside one resource.
Use for_each to create multiple resources.
Dynamic blocks loop over a list to generate blocks.
For_each on resource duplicates whole resource.
Choose dynamic blocks for nested repeated config.
Choose for_each for multiple similar resources.
Full Transcript
This visual execution shows how Terraform decides between dynamic blocks and for_each. It starts by identifying repeated configuration. If nested blocks repeat inside one resource, dynamic blocks are used. If whole resources repeat, for_each is used on the resource. The example traces a security group resource with a dynamic ingress block looping over two rules. Each iteration creates one ingress nested block with a from_port value. After generating all blocks, the resource is ready for deployment. Variables track the list of ingress rules and current loop value. Key moments clarify that dynamic blocks create repeated nested blocks inside one resource, while for_each duplicates entire resources. The quiz tests understanding of loop values, step completion, and how changing input affects execution. The snapshot summarizes when to use dynamic blocks versus for_each in Terraform.