Dynamic Blocks for Nested Configuration
Generate repeated nested configuration blocks programmatically using dynamic blocks, eliminating copy-paste for things like security group rules.
Dynamic Blocks for Nested Configuration is a free Terraform Infrastructure as Code lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Terraform Infrastructure as Code learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Repetition Problem
Some resources contain repeated nested blocks, such as multiple ingress rules in a security group. Writing each by hand is verbose and hard to keep DRY. Dynamic blocks generate them from a collection.
Static Blocks First
Here is the manual version: three near-identical ingress blocks. Imagine maintaining a dozen of these.
resource "aws_security_group" "web" {
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}Anatomy of a Dynamic Block
A dynamic block has a label matching the nested block name, an for_each collection, and a content block describing each generated instance.
dynamic "ingress" {
for_each = var.rules
content {
from_port = ingress.value.port
to_port = ingress.value.port
protocol = "tcp"
cidr_blocks = ingress.value.cidrs
}
}The Iterator Object
Inside content, refer to the current element via an object named after the block label. It exposes .value (the element) and .key (the map key or list index).
Defining the Input Variable
Drive the dynamic block with a typed variable. A list of objects keeps each rule self-documenting.
variable "rules" {
type = list(object({
port = number
cidrs = list(string)
}))
}Putting It Together
Now the resource stays compact no matter how many rules you pass.
resource "aws_security_group" "web" {
name = "web-sg"
dynamic "ingress" {
for_each = var.rules
content {
from_port = ingress.value.port
to_port = ingress.value.port
protocol = "tcp"
cidr_blocks = ingress.value.cidrs
}
}
}Renaming the Iterator
The iterator argument lets you rename the iteration variable, useful when nesting dynamic blocks to avoid confusion.
dynamic "ingress" {
for_each = var.rules
iterator = rule
content {
from_port = rule.value.port
to_port = rule.value.port
}
}Conditionally Skipping Blocks
Feed an empty collection to produce zero blocks. A ternary on for_each makes a block optional.
dynamic "logging" {
for_each = var.enable_logging ? [1] : []
content {
target_bucket = var.log_bucket
}
}Nested Dynamic Blocks
Dynamic blocks can nest. Use distinct iterators so the inner block can reference both levels without ambiguity.
dynamic "setting" {
for_each = var.settings
iterator = s
content {
namespace = s.value.namespace
name = s.value.name
value = s.value.value
}
}When Not to Use Dynamic Blocks
Dynamic blocks add indirection and can hurt readability. If you only have one or two static blocks, keep them literal. Reach for dynamic blocks when the count is data-driven.
Inspecting the Result
Run a plan to confirm the generated blocks match expectations. Terraform expands every element of the collection into a concrete nested block.
terraform planQuick Check
Test your dynamic block knowledge.
Recap: DRY Nested Config
Dynamic blocks let you generate nested configuration from data:
for_eachdrives iteration;contentdefines each block.- The iterator exposes
.valueand.key. - Empty collections make blocks optional.
- Use sparingly to keep configs readable.
Frequently asked questions
Is the “Dynamic Blocks for Nested Configuration” lesson free?
Yes — the full text of “Dynamic Blocks for Nested Configuration” is free to read here on the web, and the Terraform Infrastructure as Code course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Terraform Infrastructure as Code course, upgrade to CoddyKit PRO.
What will I learn in “Dynamic Blocks for Nested Configuration”?
Generate repeated nested configuration blocks programmatically using dynamic blocks, eliminating copy-paste for things like security group rules. You practise Terraform Infrastructure as Code with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Terraform Infrastructure as Code?
No prior experience is required. Terraform Infrastructure as Code on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Dynamic Blocks for Nested Configuration” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Terraform Infrastructure as Code lesson?
Yes. Every Terraform Infrastructure as Code lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Using Loops with `for_each` and `count`
- Terraform Built-in Functions
- Conditional Expressions and Splats
- Dynamic Blocks for Nested Configuration