Bird
Raised Fist0
Terraformcloud~20 mins

Splat expressions in Terraform - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Splat Expression Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ service_behavior
intermediate
2:00remaining
Understanding Splat Expression Output
Given the following Terraform resource block, what will be the output of output.instances_ips?
Terraform
resource "aws_instance" "example" {
  count         = 3
  ami           = "ami-123456"
  instance_type = "t2.micro"
  tags = {
    Name = "example-${count.index}"
  }
}

output "instances_ips" {
  value = aws_instance.example[*].public_ip
}
A["ami-123456", "ami-123456", "ami-123456"]
B[null, null, null]
C["example-0", "example-1", "example-2"]
D["t2.micro", "t2.micro", "t2.micro"]
Attempts:
2 left
💡 Hint
The public_ip attribute is only set after the instance is created and if it has a public IP assigned.
❓ Configuration
intermediate
2:00remaining
Correct Use of Splat Expression in Resource Arguments
Which option correctly uses a splat expression to assign all subnet IDs from a list of subnets to a security group rule's subnet_ids argument?
Terraform
resource "aws_subnet" "example" {
  count = 2
  vpc_id     = "vpc-123456"
  cidr_block = "10.0.${count.index}.0/24"
}

resource "aws_security_group_rule" "example" {
  type        = "ingress"
  from_port   = 80
  to_port     = 80
  protocol    = "tcp"
  security_group_id = "sg-123456"
  subnet_ids  = ???
}
Aaws_subnet.example.id
Baws_subnet.example.id[*]
Caws_subnet.example[*].id
Daws_subnet.example[*].subnet_id
Attempts:
2 left
💡 Hint
The splat expression syntax is resource_name[*].attribute to get a list of attribute values.
❓ Architecture
advanced
2:00remaining
Splat Expressions in Complex Module Outputs
You have a module that creates multiple EC2 instances and outputs their private IPs as private_ips. Which option correctly references all private IPs from the module using a splat expression?
Terraform
module "web_servers" {
  source = "./modules/ec2_instances"
  instance_count = 3
}

output "all_private_ips" {
  value = ???
}
Amodule.web_servers.private_ips
Bmodule.web_servers.private_ips[*]
Cmodule.web_servers[*].private_ips
Dmodule.web_servers[*].private_ips[*]
Attempts:
2 left
💡 Hint
Modules are referenced by name and their outputs are accessed directly without splat unless the module is called multiple times with count or for_each.
❓ security
advanced
2:00remaining
Splat Expression Impact on Security Group Rules
You want to create security group rules for multiple instances using their private IPs. Which splat expression usage will cause a Terraform plan error?
Terraform
resource "aws_instance" "app" {
  count         = 2
  ami           = "ami-abc123"
  instance_type = "t3.micro"
}

resource "aws_security_group_rule" "allow_app" {
  type              = "ingress"
  from_port         = 8080
  to_port           = 8080
  protocol          = "tcp"
  security_group_id = "sg-abc123"
  cidr_blocks       = ???
}
Aaws_instance.app.private_ip[*]
Baws_instance.app[*].private_ip
Caws_instance.app.private_ip
Daws_instance.app[*].private_ip[*]
Attempts:
2 left
💡 Hint
Check the correct syntax for splat expressions on resource attributes.
🧠 Conceptual
expert
3:00remaining
Behavior of Nested Splat Expressions
Consider a resource with nested blocks and a splat expression used to extract a nested attribute list. What is the output of the following expression?
Terraform
resource "aws_lb" "example" {
  name               = "example-lb"
  internal           = false
  load_balancer_type = "application"

  subnet_mapping {
    subnet_id     = "subnet-123"
    allocation_id = "eipalloc-123"
  }

  subnet_mapping {
    subnet_id     = "subnet-456"
    allocation_id = "eipalloc-456"
  }
}

output "allocation_ids" {
  value = aws_lb.example[*].subnet_mapping[*].allocation_id
}
A[["subnet-123", "subnet-456"]]
B["eipalloc-123", "eipalloc-456"]
C[null, null]
D[["eipalloc-123", "eipalloc-456"]]
Attempts:
2 left
💡 Hint
Nested splat expressions produce nested lists reflecting the structure of the nested blocks.

Practice

(1/5)
1.

What does a splat expression in Terraform do?

Example: aws_instance.example[*].id

easy
A. Collects the id attribute from all aws_instance.example resources into a list
B. Creates a new resource named id for each instance
C. Deletes all aws_instance.example resources
D. Filters instances with id equal to example

Solution

  1. Step 1: Understand the splat operator *

    The * means 'all items' in the list of resources.
  2. Step 2: Collect attribute values

    The expression aws_instance.example[*].id collects the id attribute from every instance in aws_instance.example.
  3. Final Answer:

    Collects the id attribute from all aws_instance.example resources into a list -> Option A
  4. Quick Check:

    Splat expression collects attributes [OK]
Hint: Remember: * means all items, collect their attributes [OK]
Common Mistakes:
  • Thinking splat creates or deletes resources
  • Confusing splat with filtering
  • Assuming splat returns a single value
2.

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?

easy
A. aws_s3_bucket.buckets[*].name
B. aws_s3_bucket.buckets.name[*]
C. aws_s3_bucket.buckets[].name
D. aws_s3_bucket.buckets.name

Solution

  1. Step 1: Identify splat operator position

    The splat operator * must come immediately after the resource list name inside square brackets.
  2. Step 2: Confirm attribute access

    The correct syntax is resource_list[*].attribute, so aws_s3_bucket.buckets[*].name is correct.
  3. Final Answer:

    aws_s3_bucket.buckets[*].name -> Option A
  4. Quick Check:

    Splat syntax = resource_list[*].attribute [OK]
Hint: Put * inside brackets right after resource list name [OK]
Common Mistakes:
  • Placing * after attribute name
  • Using [] without * inside
  • Omitting brackets for splat
3.

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?

medium
A. A list of 0 elements because no instances are created
B. A list of 3 instance IDs, one for each created instance
C. An error because splat cannot be used with count
D. A single instance ID of the first instance

Solution

  1. Step 1: Understand resource count creates multiple instances

    The count = 3 creates 3 instances named aws_instance.web[0], aws_instance.web[1], and aws_instance.web[2].
  2. Step 2: Splat expression collects all instance IDs

    The expression aws_instance.web[*].id collects the id attribute from all 3 instances into a list.
  3. Final Answer:

    A list of 3 instance IDs, one for each created instance -> Option B
  4. Quick Check:

    count 3 + splat = list of 3 IDs [OK]
Hint: count creates multiple; splat collects all attributes [OK]
Common Mistakes:
  • Thinking splat fails with count
  • Expecting single ID instead of list
  • Assuming no instances created
4.

Identify the error in this Terraform snippet using splat expressions:

output "bucket_names" {
  value = aws_s3_bucket.buckets.name[*]
}
medium
A. Output blocks cannot use splat expressions
B. The resource name aws_s3_bucket.buckets is invalid
C. Missing quotes around the attribute name
D. The splat operator * is incorrectly placed after the attribute name

Solution

  1. Step 1: Check splat operator position

    The splat operator * must be inside brackets immediately after the resource list, not after the attribute.
  2. Step 2: Correct syntax

    The correct syntax is aws_s3_bucket.buckets[*].name, not aws_s3_bucket.buckets.name[*].
  3. Final Answer:

    The splat operator * is incorrectly placed after the attribute name -> Option D
  4. Quick Check:

    Splat must be after resource list, not attribute [OK]
Hint: Put * right after resource list in brackets, not after attribute [OK]
Common Mistakes:
  • Placing * after attribute name
  • Confusing resource and attribute order
  • Thinking output blocks disallow splat
5.

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?

hard
A. aws_instance.web[*].public_ip
B. aws_instance.web[*].tags.Environment == "production" ? aws_instance.web[*].public_ip : []
C. [for i in aws_instance.web : i.public_ip if i.tags.Environment == "production"]
D. aws_instance.web[?tags.Environment=="production"].public_ip

Solution

  1. Step 1: Understand splat collects all attributes without filtering

    The expression aws_instance.web[*].public_ip collects all public IPs, no filtering.
  2. 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"].
  3. Final Answer:

    [for i in aws_instance.web : i.public_ip if i.tags.Environment == "production"] -> Option C
  4. Quick Check:

    Filter with for-expression + if, not splat alone [OK]
Hint: Use for-expression with if to filter, splat alone can't filter [OK]
Common Mistakes:
  • Using splat without filtering
  • Trying to filter inside splat syntax
  • Using invalid filter syntax
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard