DEV Community

1suleyman
1suleyman

Posted on

🧱 Terraform Dynamic Blocks and Validation — Clean Code, Fewer Errors, Happy Cloud

Hey everyone 👋

If you’ve started learning Terraform, you’ve probably felt the pain of writing out the same block of code again and again — or dealing with confusing errors because of one small typo.

In this post, I’ll walk you through two super useful Terraform features that can make your code cleaner and more reliable:

  1. Dynamic Blocks — to help you avoid writing the same code 40 times
  2. Terraform Validate — your syntax spell-checker for cloud infrastructure

Let’s break it down like we’re building Lego 🧱👇


🌀 Dynamic Blocks: Don’t Repeat Yourself (DRY)

Imagine you're setting up 10 doors in a virtual building. Each door represents a port (like HTTP, SSH, etc.), and each port needs a rule in your security group.

Writing that out manually?

ingress {
  from_port   = 80
  to_port     = 80
  ...
}
ingress {
  from_port   = 443
  to_port     = 443
  ...
}
... 10 more times ...
Enter fullscreen mode Exit fullscreen mode

💀 It's long. Repetitive. And easy to mess up.

🧠 Dynamic Blocks to the Rescue

With dynamic blocks, you loop through a list and automatically generate those nested blocks.

variable "ports" {
  default = [80, 443, 22]
}

resource "aws_security_group" "dynamic_sg" {
  name = "dynamic-sg"

  dynamic "ingress" {
    for_each = var.ports
    content {
      from_port   = ingress.value
      to_port     = ingress.value
      protocol    = "tcp"
      cidr_blocks = ["0.0.0.0/0"]
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

☁️ Terraform creates one ingress rule for each port in your list. Clean, scalable, and less room for typos.


🔁 Use an Iterator (Optional, but Nicer)

Want to make your code more readable?

dynamic "ingress" {
  for_each = var.ports
  iterator = port
  content {
    from_port = port.value
    ...
  }
}
Enter fullscreen mode Exit fullscreen mode

💡 Analogy: Instead of calling every guest “person,” you call them “John,” “Sarah,” etc. It just makes things easier to follow.


🧪 terraform validate: Your Syntax Safety Net

So now your code looks good. But is it valid Terraform?

This is where terraform validate comes in.

terraform validate
Enter fullscreen mode Exit fullscreen mode

It checks:

✅ Is your syntax correct?
✅ Are there any unsupported arguments?
✅ Did you forget to declare a variable?

It’s like a spell-checker for your .tf files — catching silly mistakes before you run terraform plan or apply.


🧼 Real-World Example

You write:

resource "aws_instance" "web" {
  ami           = "ami-123456"
  instance_type = var.instancetype
  sky           = "blue"  # 😬 typo
}
Enter fullscreen mode Exit fullscreen mode

Run:

terraform validate
Enter fullscreen mode Exit fullscreen mode

And you get:

Error: Unsupported argument
sky = "blue" is not supported here.
Enter fullscreen mode Exit fullscreen mode

🎯 You instantly know what went wrong — and where.


🤯 Undeclared Variables? It’ll Catch Those Too

If you forgot to define var.instancetype in your variables file:

terraform validate
Enter fullscreen mode Exit fullscreen mode

Will say:

Error: Reference to undeclared input variable
Enter fullscreen mode Exit fullscreen mode

🆚 terraform validate vs terraform plan

Command What It Does When To Use It
terraform validate Checks syntax only After writing/editing code
terraform plan Shows what will change in real infrastructure Before applying changes

🔑 Why This Matters (Especially in Teams)

  • Dynamic blocks make your code shorter and smarter
  • Validate helps you catch bugs before they cost time or money
  • Both tools help you write clean, readable, and production-ready Terraform

🧩 Final Thoughts

If you’re just starting out with Terraform, these two tools can seriously level up your workflow.

Use dynamic blocks when you see repeated nested configs (like ports, tags, or rules).
Use terraform validate before every plan or pull request.

Small tools. Big impact. 🛠️


If you’re learning Terraform too, let’s connect on LinkedIn!

Let’s learn cloud the right way — together 🚀

Top comments (0)