Day 10 of the 30 Days of AWS Terraform series dives into Terraform Expressions β a powerful concept that helps eliminate repetitive code and makes Terraform configurations dynamic, flexible, and scalable.
Expressions allow Terraform to decide, iterate, and extract values intelligently, without relying on external scripts or complex logic.
_
This session focused on three essential Terraform expressions:
Conditional expressions
Dynamic blocks
Splat expressions_
Together, these form the backbone of clean, production-ready Terraform code.
πΉ What Are Terraform Expressions?
Terraform expressions are inline constructs that evaluate values dynamically during execution.
They:
Reduce duplication
Improve reusability
Adapt infrastructure based on input values
Work seamlessly with variables, meta-arguments, and resources
π Expression Types Covered
1οΈβ£ Conditional Expressions
Conditional expressions act like a one-line ifβelse statement.
Syntax
condition ? true_value : false_value
Practical Use Case
Setting instance types based on environment:
instance_type = var.env == "dev" ? "t2.micro" : "t3.medium"
Demo Insight
When env = dev, Terraform plans a smaller instance
When env = stage, Terraform switches automatically
No code duplication required
β
Best for environment-specific logic
β
Keeps code concise and readable
2οΈβ£ Dynamic Blocks β Eliminating Repetition
Dynamic blocks are used to generate nested blocks dynamically, especially when the number of blocks depends on input data.
Problem
Manually writing multiple ingress rules in a security group:
ingress { ... }
ingress { ... }
ingress { ... }
This does not scale.
Solution: Dynamic Block
dynamic "ingress" {
for_each = var.ingress_rules
content {
from_port = ingress.value.from_port
to_port = ingress.value.to_port
protocol = ingress.value.protocol
cidr_blocks = ingress.value.cidr_blocks
description = ingress.value.description
}
}
Variable Example (List of Objects)
variable "ingress_rules" {
type = list(object({
from_port = number
to_port = number
protocol = string
cidr_blocks = list(string)
description = string
}))
}
Key Learnings
Dynamic blocks replace repetitive nested blocks
Ideal for security groups, listeners, rules, policies
Requires careful attention to data types and docs
3οΈβ£ Splat Expressions β Extracting Values Efficiently
Splat expressions are used to collect attributes from multiple resource instances created using count or for_each.
Example
Creating multiple EC2 instances with count:
resource "aws_instance" "web" {
count = 2
...
}
Splat Expression
aws_instance.web[*].id
Output Example
output "instance_ids" {
value = aws_instance.web[*].id
}
Terraform automatically:
Iterates through all instances
Collects IDs into a list
Outputs them cleanly
_
β
Clean aggregation
β
No manual loops
β
Ideal for outputs and locals_
π Conclusion
Day 10 marks a shift from basic Terraform to intelligent infrastructure design.
Terraform expressions enable:
Cleaner code
Dynamic behavior
Better scalability
Reduced duplication
Production-level configurations
Mastering conditional expressions, dynamic blocks, and splat expressions is a major step toward writing professional, maintainable Terraform code.
Next up:
π Advanced Iteration Patterns & Terraform Internals π
Top comments (0)