Splat Expressions in Terraform

https://abhaypatil001.github.io/abhay-portfolio/
Search for a command to run...

https://abhaypatil001.github.io/abhay-portfolio/
No comments yet. Be the first to comment.
Introduction User access management is one of the most fundamental responsibilities in any infrastructure environment, yet it is often handled in an ad hoc and manual way. In many organizations, espec

Managing sensitive credentials like API keys across multiple GitHub repositories is challenging. It leads to duplicate secrets, a lack of versioning, and potential inconsistencies between environments. The solution is to use HashiCorp Vault Secrets o...

Introduction DevOps engineers today spend countless hours context-switching between dashboards, logs, cloud consoles, and documentation. Traditional AI tools like ChatGPT are great at answering questions, but they lack direct access to your infrastru...

When working with GitHub Actions, you may face situations where multiple workflow runs overlap, consume unnecessary resources, or even cause conflicts in deployment. This is where concurrency comes into play. Concurrency in GitHub Actions allows you ...

When teams scale, managing workflows across multiple repositories becomes more complex. Each project often shares common configuration values, cloud credentials, or deployment secrets. Instead of duplicating these across repositories, GitHub provides...

CloudDecode
11 posts
CloudDecode simplifies cloud & DevOps—covering Azure, AWS, Kubernetes, Terraform, CI/CD & more—with clear guides to help you decode, learn, and build with confidence.
When writing Terraform code to manage cloud infrastructure, we often deal with multiple similar resources — for example, a group of EC2 instances, multiple storage accounts, or several subnets.
So how do we easily access the same attribute (like ID or name) from all of them?
This is where Terraform’s splat expressions shine.
In this blog, we’ll break down:
What splat expressions are
When and why to use them
Simple examples using count and for_each
When to use for loop instead
Tips and best practices
In Terraform, a splat expression uses the asterisk symbol (*) to quickly extract values from a list or map of resources.
In short: Use [*] to grab the same property from all resources in a group*.*
count to Create Multiple ResourcesLet’s say you want to create 3 EC2 instances.
resource "aws_instance" "example" {
count = 3
ami = "ami-12345678"
instance_type = "t2.micro"
}
Each instance will have its own ID, IP, and so on.
Now, if you want to get all 3 instance IDs, instead of writing:
[
aws_instance.example[0].id,
aws_instance.example[1].id,
aws_instance.example[2].id
]
You can just write:
output "instance_ids" {
value = aws_instance.example[*].id
}
This is a splat expression.
It goes through every instance in the list and pulls out the .id value — and gives you a list like:
["i-abc123", "i-def456", "i-ghi789"]
for_each to Create Multiple ResourcesLet’s create multiple S3 buckets using for_each:
resource "aws_s3_bucket" "example" {
for_each = toset(["my-bucket-1", "my-bucket-2"])
bucket = each.key
acl = "private"
}
Now you want the IDs of all buckets. Since for_each uses a map, you can’t directly use the [*] shortcut. Instead, combine it with values():
output "bucket_ids" {
value = values(aws_s3_bucket.example)[*].id
}
Here:
values(...) turns the map into a list
[*].id extracts all IDs from that list
Behind the Scenes — What Does [*] Do?
A splat expression is just a shortcut. It’s equivalent to a loop that collects a specific attribute from every item in the list.
So this:
aws_instance.example[*].id
Is roughly like:
[for instance in aws_instance.example : instance.id]
When to Use a for Expression Instead
If you want to do something more custom, like filtering or transforming values, use a for loop:
output "instance_names" {
value = [for inst in aws_instance.example : "instance-${inst.id}"]
}
This gives you a custom list like:
["instance-i-abc123", "instance-i-def456", ...]
FeatureDescription[*] (splat)Shortcut to extract a field from a list of resourcesWith countDirectly use resource_name[*].fieldWith for_eachUse values(resource_name)[*].fieldPrefer for loopWhen you need filtering or custom formatting
If you’re working with many dynamic resources, splat expressions make your Terraform code clean, readable, and easy to maintain.
They save time and reduce repetition — especially when you’re managing infrastructure at scale.