0
0
Terraformcloud~5 mins

Splat expressions in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you create many similar resources in Terraform and want to get a list of their attributes easily. Splat expressions help you collect these attributes from multiple resources into one list without writing each one manually.
When you create multiple virtual machines and want to get their IP addresses in a list.
When you have several storage buckets and need to list their names for output.
When you want to pass a list of resource IDs to another resource without typing each ID.
When you want to simplify your code by avoiding repeating similar attribute references.
When you want to output a list of all created resource names for documentation or monitoring.
Config File - main.tf
main.tf
resource "aws_instance" "example" {
  count         = 3
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}

output "instance_ids" {
  value = aws_instance.example[*].id
}

This Terraform file creates three AWS EC2 instances using the count feature.

The output instance_ids uses a splat expression aws_instance.example[*].id to collect the IDs of all three instances into a list.

Commands
This command initializes the Terraform working directory, downloads necessary provider plugins, and prepares for deployment.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure.
This command applies the Terraform configuration to create the resources defined in the file without asking for confirmation.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_instance.example[0]: Creating... aws_instance.example[1]: Creating... aws_instance.example[2]: Creating... aws_instance.example[0]: Creation complete after 10s [id=i-0123456789abcdef0] aws_instance.example[1]: Creation complete after 10s [id=i-0123456789abcdef1] aws_instance.example[2]: Creation complete after 10s [id=i-0123456789abcdef2] Apply complete! Resources: 3 added, 0 changed, 0 destroyed. Outputs: instance_ids = [ "i-0123456789abcdef0", "i-0123456789abcdef1", "i-0123456789abcdef2" ]
-auto-approve - Skip interactive approval before applying changes
This command shows the list of instance IDs collected by the splat expression in the output.
Terminal
terraform output instance_ids
Expected OutputExpected
["i-0123456789abcdef0", "i-0123456789abcdef1", "i-0123456789abcdef2"]
Key Concept

If you remember nothing else from this pattern, remember: splat expressions let you gather the same attribute from many resources into one simple list.

Common Mistakes
Using dot notation like aws_instance.example.id without the splat operator when multiple resources exist.
This causes an error because Terraform expects a single resource but finds many.
Use the splat expression aws_instance.example[*].id to get a list of all IDs.
Forgetting the asterisk (*) inside the square brackets in the splat expression.
Without the asterisk, Terraform does not treat it as a splat and will fail or return wrong data.
Always include the asterisk like aws_instance.example[*].attribute to collect attributes.
Summary
Use splat expressions with [*] to collect attributes from multiple resources into a list.
Apply Terraform configuration to create resources and see the collected output.
Use terraform output to view the list generated by the splat expression.