Advanced Terraform Metaprogramming: for_each, Dynamic Blocks & Complex Maps
· Sourabh G Kulkarni
Why count breaks index tracking, how to master for_each, and crafting clean dynamic blocks with try() and can() defensive patterns.

Every engineer starting with Terraform encounters count:
# ❌ The Index Shift Trap
resource "aws_subnet" "public" {
count = length(var.subnet_cidrs)
cidr_block = var.subnet_cidrs[count.index]
}
It works smoothly—until you delete the first element in var.subnet_cidrs. Because count assigns resources array-based integer indexes (aws_subnet.public[0], aws_subnet.public[1], aws_subnet.public[2]), removing index 0 causes Terraform to destroy and recreate every subsequent resource in the array. In production, this causes unintentional database or subnet destruction.
Here is how to master Terraform metaprogramming using for_each, dynamic blocks, and defensive HCL patterns.
flowchart TD
subgraph Input["Configuration Map (Key-Value)"]
Map["{ 'us-east-1a': '10.0.1.0/24', 'us-east-1b': '10.0.2.0/24' }"]
end
subgraph ForEach["for_each Execution Engine"]
Key1["aws_subnet.public['us-east-1a']"]
Key2["aws_subnet.public['us-east-1b']"]
end
subgraph Dynamic["Dynamic Block Generator"]
Rules["Security Group Rules (Ingress)"]
end
Map --> ForEach
ForEach --> Key1
ForEach --> Key2
Key1 -.-> Dynamic
1. Replacing count with for_each and Set/Map Lookups
for_each binds state identity to a unique string key, not an array index:
variable "subnets" {
type = map(object({
cidr_block = string
az = string
is_public = bool
}))
default = {
"web-a" = { cidr_block = "10.0.1.0/24", az = "us-east-1a", is_public = true }
"web-b" = { cidr_block = "10.0.2.0/24", az = "us-east-1b", is_public = true }
"db-a" = { cidr_block = "10.0.10.0/24", az = "us-east-1a", is_public = false }
}
}
resource "aws_subnet" "tiered" {
for_each = var.subnets
cidr_block = each.value.cidr_block
availability_zone = each.value.az
tags = {
Name = "subnet-${each.key}"
Tier = each.value.is_public ? "public" : "private"
}
}
Now, removing "web-a" removes only aws_subnet.tiered["web-a"]. The remaining subnets remain completely untouched.
2. Dynamic Blocks for Nested Infrastructure
When a resource requires repetitive nested attributes (such as ingress rules on an aws_security_group), never repeat blocks. Use dynamic:
locals {
service_ports = [
{ port = 80, description = "HTTP web traffic", protocol = "tcp" },
{ port = 443, description = "HTTPS web traffic", protocol = "tcp" },
{ port = 9090, description = "Prometheus metrics", protocol = "tcp" }
]
}
resource "aws_security_group" "api_gateway" {
name = "api-gateway-sg"
description = "Managed ingress rules for public gateway"
vpc_id = var.vpc_id
dynamic "ingress" {
for_each = local.service_ports
content {
description = ingress.value.description
from_port = ingress.value.port
to_port = ingress.value.port
protocol = ingress.value.protocol
cidr_blocks = ["0.0.0.0/0"]
}
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
3. Defensive HCL: try() and can() Patterns
Real-world platform modules ingest deeply nested maps where specific keys might be missing. Using var.config.monitoring.enabled directly causes runtime crashes if monitoring is null.
Use try() to provide clean fallbacks:
locals {
# Defensive lookup: if monitoring or port is omitted, safely fall back to 8080
metrics_port = try(var.service_config.monitoring.port, 8080)
# Boolean check without runtime exceptions
has_custom_kms = can(var.storage_config.kms_key_arn != null)
}
The Rule of Thumb
- Use
countstrictly for binary toggles (count = var.enable_waf ? 1 : 0). - Use
for_eachfor resource collections where elements can be added or deleted. - Use
dynamicto keep complex nested configurations DRY and maintainable.