Why dynamic blocks reduce repetition in Terraform - Performance Analysis
We want to see how using dynamic blocks in Terraform affects the number of operations as we add more repeated configurations.
How does the work grow when we avoid writing repeated blocks manually?
Analyze the time complexity of this Terraform snippet using a dynamic block.
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 multiple ingress rules inside one security group using a dynamic block looping over input rules.
Look at what repeats when Terraform applies this configuration.
- Primary operation: Creating each ingress rule inside the security group.
- How many times: Once per item in
var.ingress_rules.
As you add more ingress rules, Terraform creates more ingress blocks dynamically.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 ingress rule creations |
| 100 | 100 ingress rule creations |
| 1000 | 1000 ingress rule creations |
Pattern observation: The number of operations grows directly with the number of rules.
Time Complexity: O(n)
This means the work grows in a straight line with the number of repeated blocks you define.
[X] Wrong: "Using dynamic blocks makes Terraform do fewer operations overall."
[OK] Correct: Dynamic blocks just help write less code, but Terraform still creates each resource or sub-block once per item.
Understanding how repeating configurations scale helps you design efficient infrastructure code and explain your choices clearly.
"What if we replaced the dynamic block with multiple static blocks written manually? How would the time complexity change?"
Practice
dynamic blocks in Terraform?Solution
Step 1: Understand the purpose of dynamic blocks
Dynamic blocks let you create repeated nested blocks from a list or map without manually copying code.Step 2: Compare benefits
This reduces repetition and keeps code clean and scalable, unlike options about speed, variables, or backend.Final Answer:
They reduce repetition by generating multiple nested blocks automatically. -> Option CQuick Check:
Dynamic blocks reduce repetition = C [OK]
- Thinking dynamic blocks speed up Terraform execution
- Confusing dynamic blocks with variables
- Believing dynamic blocks remove backend needs
Solution
Step 1: Recall dynamic block syntax
Dynamic blocks use the keyworddynamicfollowed by the block name in quotes, then a block withfor_each.Step 2: Match syntax to options
Only dynamic "block_name" { for_each = var.list } matches the correct syntax:dynamic "block_name" { for_each = var.list }.Final Answer:
dynamic "block_name" { for_each = var.list } -> Option AQuick Check:
Correct dynamic block syntax = D [OK]
- Omitting quotes around block name
- Using equals sign instead of braces
- Placing for_each outside the block
variable "ports" { default = [80, 443] }
resource "aws_security_group" "example" {
name = "example"
dynamic "ingress" {
for_each = var.ports
content {
from_port = ingress.value
to_port = ingress.value
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
}How many
ingress blocks will be created in the security group?Solution
Step 1: Analyze the for_each list
The variableportscontains two values: 80 and 443.Step 2: Count dynamic block iterations
The dynamic block creates oneingressblock per item invar.ports, so 2 blocks total.Final Answer:
2 -> Option DQuick Check:
List length = number of blocks = 2 [OK]
- Assuming only one block is created
- Thinking AWS region affects block count
- Confusing dynamic block with static block
dynamic "egress" {
for_each = var.rules
content = {
from_port = egress.value.from
to_port = egress.value.to
protocol = "tcp"
}
}Solution
Step 1: Check dynamic block content syntax
Thecontentmust be a block, not assigned with equals sign.Step 2: Identify correct syntax
Correct syntax usescontent { ... }without equals sign. The snippet incorrectly usescontent = { ... }.Final Answer:
Incorrect use of equals sign for content block -> Option AQuick Check:
content block uses braces, no equals sign = B [OK]
- Using equals sign with content
- Placing for_each outside dynamic block
- Misquoting block name
lifecycle_rule blocks in an aws_s3_bucket resource using a dynamic block. Your variable is a map with keys as rule names and values as rule details. Which approach correctly uses dynamic blocks to reduce repetition and handle the map structure?Solution
Step 1: Understand dynamic block with map variable
When using a map,for_eachiterates over key-value pairs. The value is accessed withlifecycle_rule.value.Step 2: Identify correct usage
Usefor_each = var.rulesand access each rule withlifecycle_rule.valueinside content block. correctly usesfor_each = var.rulesand accesseslifecycle_rule.valueinside the content block to get rule details.Final Answer:
Use for_each = var.rules and access each rule with lifecycle_rule.value inside content block. -> Option BQuick Check:
Map iteration uses .value for content = A [OK]
- Using only .key without .value for map data
- Using count instead of for_each for maps
- Writing blocks manually instead of dynamic
Ready to prove it?
Pass the test and this pattern moves unknown
