Day 10 was one of those days where Terraform started to feel less like static configuration and more like real logic. Until now, I was mostly writing fixed resources. Today, I learned how to make Terraform react to conditions, generate blocks dynamically, and handle multiple resources cleanly.
This session focused on three important concepts: conditional expressions, dynamic blocks, and splat expressions.
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 were one of the most interesting parts of today’s learning. They allow Terraform to generate repeated blocks automatically, instead of writing the same configuration multiple times.
This is especially useful when a resource has nested blocks like 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
}
}
What I liked about this is how clean the configuration becomes. Instead of repeating the same block again and again, Terraform handles it in a loop-like way.
- Dynamic blocks make the code:
- 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.
Splat expressions are very helpful when:
- Using count or for_each
- Passing multiple values to outputs
- Avoiding manual indexing
It made my code feel cleaner and easier to understand.
What I Understood After Practicing
Before today, I was writing Terraform in a very fixed way. After learning these concepts:
- 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 made these concepts much clearer than just reading about them.
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
Top comments (0)