0
0
Terraformcloud~10 mins

Dynamic blocks in ingress rules in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Dynamic blocks in ingress rules
Start Terraform config
Define ingress rules list
Enter resource block
Use dynamic block to loop over ingress rules
For each rule: create ingress block
Terraform applies config
Ingress rules created dynamically
End
Terraform reads a list of ingress rules, then uses a dynamic block inside a resource to create multiple ingress entries automatically.
Execution Sample
Terraform
variable "ingress_rules" {
  default = [
    { 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"] }
  ]
}

resource "aws_security_group" "example" {
  name = "example-sg"

  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 dynamically creates two ingress rules in a security group using a dynamic block looping over a list.
Process Table
StepActionDynamic Block IterationIngress Rule CreatedResource State
1Start Terraform applyN/ANoSecurity group resource initialized, no ingress rules yet
2Evaluate dynamic blockIteration 1Ingress rule with from_port=80, to_port=80, protocol=tcp, cidr_blocks=["0.0.0.0/0"]Ingress block 1 added to security group
3Evaluate dynamic blockIteration 2Ingress rule with from_port=443, to_port=443, protocol=tcp, cidr_blocks=["0.0.0.0/0"]Ingress block 2 added to security group
4Finish applyN/AAll ingress rules createdSecurity group with 2 ingress rules ready
💡 All ingress rules from the list processed, dynamic block iterations complete
Status Tracker
VariableStartAfter Iteration 1After Iteration 2Final
var.ingress_rules[{from_port=80,...},{from_port=443,...}]SameSameSame
ingress.valueN/A{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"]}N/A
aws_security_group.example.ingressEmpty1 ingress rule added2 ingress rules added2 ingress rules total
Key Moments - 3 Insights
Why does Terraform create multiple ingress blocks from one dynamic block?
Because the dynamic block loops over each item in var.ingress_rules, creating one ingress block per item as shown in execution_table rows 2 and 3.
What happens if var.ingress_rules is empty?
The dynamic block has no items to loop over, so no ingress blocks are created. This is implied by the loop behavior in execution_table where iterations correspond to list items.
How does Terraform know which values to assign inside each ingress block?
Inside the dynamic block, ingress.value refers to the current item in the list, so its properties (from_port, to_port, etc.) are assigned to the block fields as shown in the code and execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the from_port value in iteration 2 of the dynamic block?
A443
B22
C80
D0
💡 Hint
Check execution_table row 3 under 'Ingress Rule Created' for iteration 2 details.
At which step does Terraform finish creating all ingress rules?
AStep 2
BStep 3
CStep 4
DStep 1
💡 Hint
Look at execution_table row 4 describing the finish apply step.
If var.ingress_rules had 3 items instead of 2, how would the execution_table change?
AOnly 2 iterations would run regardless
BThere would be 3 iterations with 3 ingress rules created
CTerraform would error out
DIngress rules would be merged into 2 blocks
💡 Hint
Dynamic blocks loop over all items in the list as shown in variable_tracker and execution_table.
Concept Snapshot
Terraform dynamic blocks let you create multiple nested blocks from a list.
Use dynamic "block_name" { for_each = list ... } to loop.
Inside content { ... }, use block_name.value to access each item.
This avoids repeating similar blocks manually.
Useful for ingress rules where many similar entries exist.
Full Transcript
This visual execution shows how Terraform uses dynamic blocks to create multiple ingress rules in a security group. It starts by defining a list of ingress rules. Then, inside the aws_security_group resource, a dynamic block loops over this list. For each item, Terraform creates one ingress block with the specified ports, protocol, and CIDR blocks. The execution table traces each iteration, showing how ingress rules are added step-by-step. Variables track the list and current item values. Key moments clarify why multiple blocks are created and how values are assigned. The quiz tests understanding of iteration details and dynamic block behavior. This helps beginners see how dynamic blocks automate repetitive resource definitions in Terraform.