DEV Community

Cover image for Day 10 : Conditionals, Dynamic Blocks & Splat Expressions
Zakariyau Mukhtar
Zakariyau Mukhtar

Posted on

Day 10 : Conditionals, Dynamic Blocks & Splat Expressions

Today was one of those days where everything just clicked. I didn’t just learn Terraform concepts, I finally felt like I understood why they matter and how they fit into real-world infrastructure. Compared to some earlier days where things felt abstract. Day 10 was smooth, logical, and honestly very satisfying.
I focused on three main concepts today: conditional expressions, dynamic blocks and splat expressions. All three are tools that help clean up Terraform configurations, reduce repetition and make infrastructure adapt to inputs automatically. Once I saw them in action, it finally made sense why more experienced DevOps engineers rely on them heavily.

1. Conditional Expressions (Choosing Resources Based on Environment):

The first thing I learned was how to use conditional expressions in Terraform. These allow you to change behavior based on variables. Instead of hardcoding instance types, you can make Terraform decide automatically.

In my aws_instance resource, I used:

instance_type = var.environment == "dev" ? "t2.micro" : "t3.micro"
Enter fullscreen mode Exit fullscreen mode

That one line alone felt powerful, It means:

  • If the environment is dev, use a t2.micro instance.
  • Otherwise staging, production, anything else use a t3.micro.

It removes the headache of manually editing resource blocks whenever the environment changes. Just set the environment in terraform.tfvars, run the plan, and Terraform decides for you.

In my case, I set:

environment = "staging"
instance_count = 2
Enter fullscreen mode Exit fullscreen mode

Terraform automatically picked t3.micro exactly as expected, Clean and efficient.

2. Dynamic Blocks (Generating Nested Arguments Automatically):

Next, I went deeper into dynamic blocks, which turned out to be one of the most useful features so far. Instead of manually writing multiple ingress rules inside a security group, I used a dynamic block to loop through all rules defined in ingress_rules.

My block looked like this:

dynamic "ingress" {
  for_each = var.ingress_rules
  content {
    from_port   = ingress.value.from_port
    to_port     = ingress.value.to_port
    cidr_blocks = ingress.value.cidr_blocks
    protocol    = ingress.value.protocol
  }
}
Enter fullscreen mode Exit fullscreen mode

Terraform took each rule from the variable and automatically generated the nested ingress configurations inside the security group. No duplication, No messy code, Just one clean dynamic block. The ingress_rules variable contains two rules: port 80 and port 443.
Terraform turned them into two separate ingress blocks without me writing them manually. This is one of those features that separates beginners from people who are starting to structure Terraform properly.

3. Splat Expressions (Collecting Values From Multiple Resources) :

Since I deployed multiple EC2 instances (thanks to count = var.instance_count), I had multiple instance IDs. Instead of referencing each one separately, I used a splat expression to grab all their IDs at once:

locals {
  all_instance_ids = aws_instance.example[*].id
}
Enter fullscreen mode Exit fullscreen mode

Then I output them:

output "instance_ids" {
  value = local.all_instance_ids
}
Enter fullscreen mode Exit fullscreen mode

After running:

tf init
tf plan
Enter fullscreen mode Exit fullscreen mode

Terraform clearly showed all instance IDs generated. Simple, elegant and exactly what splat expressions are meant for.

Commands I Used:

Nothing complicated today just the essentials:

  • tf init : initialize Terraform.
  • tf plan : preview changes.

5. Were the Tasks Hard?:

Not at all and that’s not because they were easy, but because every lesson before this point built up to today. By the time I started writing conditionals, dynamic blocks, and splat expressions, the syntax felt natural.

Everything was logical, predictable and connected to real Terraform use cases.

Final Thoughts

Day 10 wasn’t heavy or confusing. It was clean, organized, and practical. I used features that real DevOps engineers rely on daily:

  • Selecting resources per environment
  • Dynamically generating nested blocks
  • Collecting attributes from multiple resources

The best part is that I didn’t just follow instructions I understood why each part matters. And If i keep at this pace, Terraform won’t just be something I’m learning. It’ll be something I can confidently use to manage infrastructure the right way.

Top comments (0)