0
0
Terraformcloud~10 mins

Splat expressions in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Splat expressions
Start with list of objects
Apply splat expression: *.attribute
Terraform extracts attribute from each object
Create new list with extracted values
Use new list in configuration or output
Splat expressions take a list of objects and extract a specific attribute from each, producing a new list of those attribute values.
Execution Sample
Terraform
resource "aws_instance" "example" {
  count = 3
  ami           = "ami-123456"
  instance_type = "t2.micro"
}

output "instance_ids" {
  value = aws_instance.example[*].id
}
This code creates 3 AWS instances and outputs a list of their IDs using a splat expression.
Process Table
StepActionInput ListSplat ExpressionResulting List
1Create 3 instances[]N/A[instance0, instance1, instance2]
2Apply splat expression[instance0, instance1, instance2]*.id[id0, id1, id2]
3Output list of IDs[id0, id1, id2]N/A["i-abc123", "i-def456", "i-ghi789"]
4EndN/AN/AOutput ready with 3 instance IDs
💡 All instances created and their IDs extracted into a list for output.
Status Tracker
VariableStartAfter Step 1After Step 2Final
aws_instance.example[][instance0, instance1, instance2][instance0, instance1, instance2][instance0, instance1, instance2]
aws_instance.example[*].idN/AN/A[id0, id1, id2]["i-abc123", "i-def456", "i-ghi789"]
output.instance_idsN/AN/AN/A["i-abc123", "i-def456", "i-ghi789"]
Key Moments - 3 Insights
Why does aws_instance.example[*].id produce a list instead of a single value?
Because aws_instance.example is a list of instances, the splat expression *.id extracts the id attribute from each instance, resulting in a list of IDs as shown in execution_table step 2.
Can splat expressions be used on single objects?
No, splat expressions work on lists. If you use them on a single object, Terraform will return an error or unexpected result. See execution_table step 1 where the input is a list.
What happens if one instance does not have an id attribute?
Terraform will fail during apply because the splat expression expects all objects to have the attribute. This is implied in execution_table step 2 where all instances have ids.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what does the splat expression aws_instance.example[*].id return?
AA list of instance IDs
BA single instance ID
CA list of instance objects
DAn error
💡 Hint
Refer to the 'Resulting List' column in step 2 of execution_table.
At which step does Terraform create the actual instances?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Check the 'Action' column in execution_table for instance creation.
If the count was changed to 2, how would the output list change in the variable_tracker?
AIt would have 3 IDs
BIt would have 2 IDs
CIt would be empty
DIt would cause an error
💡 Hint
Look at the 'After Step 2' and 'Final' columns for aws_instance.example[*].id in variable_tracker.
Concept Snapshot
Splat expressions in Terraform extract attributes from each object in a list.
Syntax: resource_list[*].attribute
Returns a list of attribute values.
Useful for outputs or passing lists of values.
Works only on lists, not single objects.
Full Transcript
Splat expressions let you take a list of resources and get a list of one attribute from each. For example, if you create multiple AWS instances, you can get all their IDs in one list using aws_instance.example[*].id. This works because the splat expression applies the attribute extraction to each item in the list. The execution steps show creating instances, applying the splat, and outputting the list of IDs. Variables track the list of instances and their IDs as they change. Key points include that splat expressions only work on lists and that all objects must have the attribute. The quizzes check understanding of what the splat returns, when instances are created, and how changing count affects the output list.