0
0
Terraformcloud~10 mins

Dynamic block syntax in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Dynamic block syntax
Start Terraform config
Identify dynamic block
Iterate over collection
For each item: generate nested block
Merge generated blocks into resource
Apply Terraform plan
Resource created with dynamic nested blocks
End
Terraform reads the dynamic block, loops over the given collection, creates nested blocks for each item, and applies the configuration.
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 code dynamically creates multiple ingress rules inside an AWS security group based on the list variable 'ingress_rules'.
Process Table
StepActionInput/ConditionResultNotes
1Start processing resourceaws_security_group exampleReady to process blocksInitial resource parsing
2Identify dynamic blockdynamic "ingress"Prepare to iterate over var.ingress_rulesDynamic block found
3Evaluate for_eachvar.ingress_rules = [{from_port=80,...}, {from_port=443,...}]2 items to iterateTwo ingress rules
4Iterate item 1ingress.value = {from_port=80, to_port=80, protocol="tcp", cidr_blocks=["0.0.0.0/0"]}Generate ingress block #1First ingress rule created
5Iterate item 2ingress.value = {from_port=443, to_port=443, protocol="tcp", cidr_blocks=["0.0.0.0/0"]}Generate ingress block #2Second ingress rule created
6Merge blocksAll generated ingress blocksResource has 2 ingress blocksDynamic blocks merged
7Apply Terraform planResource with dynamic ingress blocksSecurity group created with 2 ingress rulesInfrastructure updated
8EndAll steps completeTerraform apply finishedExecution complete
💡 All items in var.ingress_rules processed, dynamic blocks generated and applied.
Status Tracker
VariableStartAfter 1After 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
Key Moments - 3 Insights
Why does Terraform create multiple ingress blocks from one dynamic block?
Because the dynamic block uses for_each to loop over a list (var.ingress_rules). Each item creates one nested ingress block, as shown in execution_table rows 4 and 5.
What happens if var.ingress_rules is empty?
No ingress blocks are generated. The dynamic block produces zero nested blocks, so the resource has no ingress rules. This is implied by the iteration step in execution_table row 3.
Can the content block inside dynamic use the loop variable?
Yes, inside content, you access the current item with ingress.value, as shown in execution_table rows 4 and 5 where ingress.value fields are used.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. How many items does var.ingress_rules contain?
A2
B1
C3
D0
💡 Hint
Check the 'Input/Condition' column at step 3 showing the list length.
At which step does Terraform generate the second ingress block?
AStep 4
BStep 5
CStep 6
DStep 7
💡 Hint
Look at the 'Action' column describing iteration over items.
If var.ingress_rules was empty, what would happen to the resource?
ATerraform would error out
BIt would create one default ingress block
CIt would have no ingress blocks
DIt would create ingress blocks from previous runs
💡 Hint
Refer to key_moments about empty lists and dynamic block behavior.
Concept Snapshot
Dynamic block syntax in Terraform:
- Use dynamic "block_name" to create nested blocks dynamically.
- for_each loops over a collection to generate multiple blocks.
- content block defines the nested block structure using loop variable.
- Useful for variable-length nested configurations.
- If collection is empty, no nested blocks are created.
Full Transcript
This visual execution traces Terraform's dynamic block syntax. Terraform starts parsing the resource and finds a dynamic block named ingress. It evaluates the for_each expression, which is a list of ingress rules. For each item in the list, Terraform generates one nested ingress block using the content block, substituting values from the current item. After processing all items, Terraform merges the generated blocks into the resource and applies the plan, creating the security group with multiple ingress rules. Variables like var.ingress_rules remain constant, while ingress.value changes per iteration. Key points include how dynamic blocks loop over collections to create multiple nested blocks, and that empty collections produce no nested blocks. The execution table shows each step clearly, helping beginners understand the flow and state changes.