Dynamic block syntax in Terraform - Time & Space Complexity
We want to understand how the time to apply Terraform changes grows when using dynamic blocks.
Specifically, how does adding more repeated blocks affect the number of operations Terraform performs?
Analyze the time complexity of this Terraform dynamic block example.
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 creates a security group with multiple ingress rules generated dynamically from a list.
Look at what repeats when Terraform applies this configuration.
- Primary operation: Creating or updating each ingress rule block inside the security group.
- How many times: Once for each item in
var.ingress_rules.
As the number of ingress rules increases, Terraform performs more operations.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | About 10 ingress rule operations |
| 100 | About 100 ingress rule operations |
| 1000 | About 1000 ingress rule operations |
Pattern observation: The number of operations grows directly with the number of ingress rules.
Time Complexity: O(n)
This means the time to apply changes grows linearly with the number of dynamic blocks.
[X] Wrong: "Adding more dynamic blocks won't affect apply time much because Terraform handles them all at once."
[OK] Correct: Each dynamic block creates separate resources or settings, so Terraform must process each one individually, increasing apply time.
Understanding how repeated resource blocks affect deployment time helps you design efficient infrastructure code and explain your choices clearly.
What if we replaced the dynamic block with a fixed number of static blocks? How would the time complexity change?
Practice
dynamic block in Terraform?Solution
Step 1: Understand the role of dynamic blocks
Dynamic blocks allow repeating nested blocks based on a collection like a list or map.Step 2: Compare with other options
Options A, B, and D describe unrelated Terraform features.Final Answer:
To generate multiple nested blocks from a list or map dynamically -> Option AQuick Check:
Dynamic block = repeated nested blocks [OK]
- Confusing dynamic blocks with variable declarations
- Thinking dynamic blocks create providers
- Assuming dynamic blocks run commands
ingress in Terraform?Solution
Step 1: Recall dynamic block syntax
The block must be declared as dynamic "block_name" { ... } with quotes around the block name.Step 2: Check each option
dynamic "ingress" { for_each = var.rules content { ... } } uses correct syntax with quotes and proper structure; others miss quotes or have wrong syntax.Final Answer:
dynamic "ingress" { for_each = var.rules content { ... } } -> Option CQuick Check:
Dynamic block syntax requires quotes around block name [OK]
- Omitting quotes around the block name
- Using parentheses instead of braces
- Placing block name outside dynamic keyword
dynamic "tag" {
for_each = {"env" = "prod", "team" = "devops"}
content {
key = tag.key
value = tag.value
}
}What will be the result in the generated resource configuration?
Solution
Step 1: Understand for_each with map in dynamic block
Using a map with for_each creates one nested block per key-value pair.Step 2: Analyze content block usage
Inside content,tag.keyandtag.valuerefer to each map entry's key and value, creating two blocks.Final Answer:
Twotagblocks with keys "env" and "team" and their values -> Option DQuick Check:
Map for_each creates one block per key-value pair [OK]
- Thinking only one block is created for whole map
- Confusing key and value references inside content
- Assuming syntax error due to map usage
dynamic "egress" {
for_each = var.egress_rules
content = {
from_port = egress.value.from
to_port = egress.value.to
}
}Solution
Step 1: Check dynamic block content syntax
Thecontentmust be a block, not an assignment. It should becontent { ... }, notcontent = { ... }.Step 2: Verify other parts
Variableegressis valid as the iterator, quotes are present, and for_each can use variables.Final Answer:
Thecontentblock should use braces, not an assignment -> Option AQuick Check:
content must be a block, not an equals assignment [OK]
- Writing content as an assignment instead of a block
- Assuming iterator variable is undefined
- Forgetting quotes around dynamic block name
network_interface blocks dynamically in an AWS instance resource using a list of maps with keys device_index and subnet_id. Which dynamic block correctly achieves this?Solution
Step 1: Identify the iterator variable in dynamic block
Terraform useseachas the iterator inside dynamic blocks forfor_each.Step 2: Check how to access map values
Useeach.value.key_nameto access map elements inside content block.Step 3: Evaluate options
dynamic "network_interface" { for_each = var.interfaces content { device_index = each.value.device_index subnet_id = each.value.subnet_id } } correctly useseach.value.device_indexandeach.value.subnet_id. Others incorrectly usenetwork_interfaceornetwork_interface.key.Final Answer:
dynamic "network_interface" { for_each = var.interfaces content { device_index = each.value.device_index subnet_id = each.value.subnet_id } } -> Option BQuick Check:
Use each.value to access map in dynamic block [OK]
- Using block name as iterator variable
- Using .key instead of .value for map access
- Omitting the each keyword
Ready to prove it?
Pass the test and this pattern moves unknown
