# Splat Expressions in Terraform

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
    

## **What Is a Splat Expression?**

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****.*

## **Scenario 1: Using** `count` to Create Multiple Resources

Let’s say you want to create 3 EC2 instances.

```plaintext
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:

```plaintext
[
  aws_instance.example[0].id,
  aws_instance.example[1].id,
  aws_instance.example[2].id
]
```

You can just write:

```plaintext
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:

```plaintext
["i-abc123", "i-def456", "i-ghi789"]
```

## **Scenario 2: Using** `for_each` to Create Multiple Resources

Let’s create multiple S3 buckets using `for_each`:

```plaintext
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()`:

```plaintext
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:

```plaintext
aws_instance.example[*].id
```

Is roughly like:

```plaintext
[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:

```plaintext
output "instance_names" {
  value = [for inst in aws_instance.example : "instance-${inst.id}"]
}
```

This gives you a custom list like:

```plaintext
["instance-i-abc123", "instance-i-def456", ...]
```

# **Summary**

FeatureDescription`[*]` (splat)Shortcut to extract a field from a list of resourcesWith `count`Directly use `resource_name[*].field`With `for_each`Use `values(resource_name)[*].field`Prefer `for` loopWhen you need filtering or custom formatting

# **Final Tip**

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.
