Splat expressions in Terraform - Commands & Configuration
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.
terraform init
terraform apply -auto-approve
-auto-approve - Skip interactive approval before applying changesterraform output instance_ids
If you remember nothing else from this pattern, remember: splat expressions let you gather the same attribute from many resources into one simple list.
Practice
What does a splat expression in Terraform do?
Example: aws_instance.example[*].id
Solution
Step 1: Understand the splat operator
The**means 'all items' in the list of resources.Step 2: Collect attribute values
The expressionaws_instance.example[*].idcollects theidattribute from every instance inaws_instance.example.Final Answer:
Collects theidattribute from allaws_instance.exampleresources into a list -> Option AQuick Check:
Splat expression collects attributes [OK]
- Thinking splat creates or deletes resources
- Confusing splat with filtering
- Assuming splat returns a single value
Which of the following is the correct syntax for a splat expression to get all name attributes from a resource list aws_s3_bucket.buckets?
Solution
Step 1: Identify splat operator position
The splat operator*must come immediately after the resource list name inside square brackets.Step 2: Confirm attribute access
The correct syntax isresource_list[*].attribute, soaws_s3_bucket.buckets[*].nameis correct.Final Answer:
aws_s3_bucket.buckets[*].name -> Option AQuick Check:
Splat syntax = resource_list[*].attribute [OK]
- Placing * after attribute name
- Using [] without * inside
- Omitting brackets for splat
Given the Terraform code snippet:
resource "aws_instance" "web" {
count = 3
ami = "ami-123456"
instance_type = "t2.micro"
}
output "instance_ids" {
value = aws_instance.web[*].id
}What will be the output of instance_ids after deployment?
Solution
Step 1: Understand resource count creates multiple instances
Thecount = 3creates 3 instances namedaws_instance.web[0],aws_instance.web[1], andaws_instance.web[2].Step 2: Splat expression collects all instance IDs
The expressionaws_instance.web[*].idcollects theidattribute from all 3 instances into a list.Final Answer:
A list of 3 instance IDs, one for each created instance -> Option BQuick Check:
count 3 + splat = list of 3 IDs [OK]
- Thinking splat fails with count
- Expecting single ID instead of list
- Assuming no instances created
Identify the error in this Terraform snippet using splat expressions:
output "bucket_names" {
value = aws_s3_bucket.buckets.name[*]
}Solution
Step 1: Check splat operator position
The splat operator*must be inside brackets immediately after the resource list, not after the attribute.Step 2: Correct syntax
The correct syntax isaws_s3_bucket.buckets[*].name, notaws_s3_bucket.buckets.name[*].Final Answer:
The splat operator*is incorrectly placed after the attribute name -> Option DQuick Check:
Splat must be after resource list, not attribute [OK]
- Placing * after attribute name
- Confusing resource and attribute order
- Thinking output blocks disallow splat
You have a list of AWS EC2 instances created with count = 4. You want to output a list of their public IPs but only for instances that have a tag Environment set to production. Which Terraform expression correctly uses splat and filtering?
Solution
Step 1: Understand splat collects all attributes without filtering
The expressionaws_instance.web[*].public_ipcollects all public IPs, no filtering.Step 2: Use for-expression with condition for filtering
The correct way to filter is using a for-expression with an if condition:[for i in aws_instance.web : i.public_ip if i.tags.Environment == "production"].Final Answer:
[for i in aws_instance.web : i.public_ip if i.tags.Environment == "production"] -> Option CQuick Check:
Filter with for-expression + if, not splat alone [OK]
- Using splat without filtering
- Trying to filter inside splat syntax
- Using invalid filter syntax
Ready to prove it?
Pass the test and this pattern moves unknown
