Splat expressions in Terraform - Time & Space Complexity
We want to understand how using splat expressions affects the number of operations Terraform performs.
Specifically, how does the work grow when we use splat expressions on multiple resources?
Analyze the time complexity of the following operation sequence.
resource "aws_instance" "example" {
count = var.instance_count
ami = var.ami_id
instance_type = "t2.micro"
}
output "instance_ids" {
value = aws_instance.example[*].id
}
This code creates multiple instances and uses a splat expression to collect their IDs into a list.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Creating each aws_instance resource and retrieving its ID.
- How many times: Once per instance, equal to the count variable.
As the number of instances increases, the number of API calls and data retrievals grows proportionally.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 instance creations + 10 ID retrievals |
| 100 | 100 instance creations + 100 ID retrievals |
| 1000 | 1000 instance creations + 1000 ID retrievals |
Pattern observation: The operations grow linearly with the number of instances.
Time Complexity: O(n)
This means the work grows directly in proportion to the number of resources you manage with the splat expression.
[X] Wrong: "Using a splat expression makes Terraform do all operations only once regardless of resource count."
[OK] Correct: Each resource still requires its own creation and data retrieval; the splat just collects results but does not reduce the number of operations.
Understanding how resource counts affect operation numbers helps you design efficient infrastructure code and explain your reasoning clearly.
"What if we replaced the splat expression with individual references to each resource ID? How would the time complexity change?"
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
