When you manage many similar resources in Terraform, you need a clear way to work with their shared attributes. A Terraform splat expression gives you a concise pattern for reading values across multiple instances so your infrastructure can scale smoothly while your configuration stays readable and easy to maintain.
What is a Terraform splat expression?
In Terraform, a splat expression is a shorthand syntax used to extract values from lists or sets of resources, typically when referencing multiple similar resources.
It uses the special [*] operator to iterate over multiple instances. For example, aws_instance.example[*].id returns a list of all id values from the aws_instance.example resources.
Splat expressions are especially useful with resources that use count, because this argument creates multiple instances in a list and you often want to read their attributes as a group. For resources that use for_each, which creates a map of instances, you normally use a for expression instead.
There are two types of splat expressions:
- Full splat (for example,
resource[*].attribute) returns a list of that attribute from every instance. - Legacy splat (for example,
aws_instance.example.*.id) behaves similarly and is still supported for backward compatibility, but it is better to use the[*]form in new Terraform code.
Splat expressions improve readability and simplify referencing attributes from multiple resource instances in outputs, dependencies, or modules.
Example 1: Collect public IPs from multiple EC2 instances
In this example, we’ll create several EC2 instances with the count argument and then use a Terraform splat expression to collect all their public IP addresses into a single output.
resource "aws_instance" "web" {
count = 3
ami = "ami-123456"
instance_type = "t3.micro"
}
output "web_public_ips" {
value = aws_instance.web[*].public_ip
}Here aws_instance.web[*].public_ip is the splat expression. It returns a list that contains the public_ip of each web instance.
After terraform apply, the web_public_ips output will be a list of three IP addresses. This pattern is common when you need all IPs for DNS records, security rules, or external monitoring tools.
In real code, replace the example AMI with a valid AMI ID or a data source so that Terraform can create real instances.
Example 2: Get all bucket names from a module output
In this example, a module creates several storage buckets and exports a list of objects that describe them, including each bucket name. You then use a splat expression on that list to work with every bucket name.
module "buckets" {
source = "./modules/buckets"
bucket_count = 4
}
output "all_bucket_names" {
value = module.buckets.bucket_names[*]
}Here buckets is a module output that exposes information about each created bucket. The expression module.buckets.bucket_names[*] returns a list with every bucket name.
You can feed this list into other modules, join it into a string, or reference it in IAM policies that must include all buckets.
Key points
Terraform splat expressions are a simple way to extract values from all elements in a list or a set of resources. When you create multiple instances of a resource with count, Terraform stores them as a list, while for_each creates a map, which usually calls for a for expression instead of a splat.
A splat expression lets you gather one specific attribute from every instance at once. This keeps your configuration clean, predictable, and easy for other people on your team to read because the pattern is consistent and readable.
Terraform is really powerful, but to achieve an end-to-end secure GitOps approach, you need to use a product that can run your Terraform workflows. Spacelift takes managing Terraform to the next level by giving you access to a powerful CI/CD workflow and unlocking features such as:
- Policies (based on Open Policy Agent)
- Multi-IaC workflows
- Self-service infrastructure
- Integrations with any third-party tools
If you want to learn more about Spacelift, create a free account today or book a demo with one of our engineers.
Note: New versions of Terraform are placed under the BUSL license, but everything created before version 1.5.x stays open-source. OpenTofu is an open-source version of Terraform that expands on Terraform’s existing concepts and offerings. It is a viable alternative to HashiCorp’s Terraform, being forked from Terraform version 1.5.6.
Automate Terraform deployments with Spacelift
Automate your infrastructure provisioning and build more complex workflows based on Terraform using policy as code, programmatic configuration, context sharing, drift detection, resource visualization, and many more.
Frequently asked questions
Is the legacy splat syntax still supported?
Yes, it is still supported for backward compatibility, but you should prefer the
[*]form because it is clearer and aligns with modern Terraform style.When should I use a splat vs a for expression?
Use a splat when you only need to collect a single attribute from each instance. Use a for expression when you need to transform values, filter items, or build maps in a more customized way.
Does splat work with both count and for_each?
Splat expressions work directly with resources that use count, because those instances form a list. Resources that use for_each form a map, so you usually read their attributes with a for expression or by turning the map into a list first.
