Day 10: Making Terraform Smarter with Conditions, Dynamic Blocks, and Splat Expressions

Published: (December 14, 2025 at 02:11 AM EST)
1 min read
Source: Dev.to

Source: Dev.to

Conditional Expressions in Terraform

Conditional expressions help Terraform make decisions based on values. Instead of writing separate resources for different environments, we can control behavior using conditions.

The basic format looks like this:

condition ? true_value : false_value

Dynamic Blocks: Writing Less, Doing More

Dynamic blocks allow Terraform to generate repeated nested blocks automatically, reducing duplication in configurations such as ingress rules or tags.

Example

dynamic "ingress" {
  for_each = var.ingress_rules
  content {
    from_port   = ingress.value.from
    to_port     = ingress.value.to
    protocol    = ingress.value.protocol
    cidr_blocks = ingress.value.cidr
  }
}

Benefits

  • Easier to maintain
  • Less repetitive
  • More flexible

Splat Expressions: Working with Multiple Resources

Splat expressions are used when multiple resources are created and we want to access their attributes together.

Example

aws_instance.example[*].id

This returns a list of all instance IDs created under that resource.

Common Uses

  • Working with count or for_each
  • Passing multiple values to outputs
  • Avoiding manual indexing

What I Understood After Practicing

  • Conditional expressions helped reduce duplicate code.
  • Dynamic blocks simplified repeated configurations.
  • Splat expressions made handling multiple resources easier.
  • Terraform felt more logical and flexible.

Hands‑on practice clarified these concepts far better than reading alone.

Key Takeaways from Day 10

  • Terraform supports conditional logic.
  • Dynamic blocks reduce repetition.
  • Splat expressions simplify multi‑resource outputs.
  • These features make Terraform more scalable.
  • Clean logic leads to clean infrastructure code.
Back to Blog

Related posts

Read more »