Challenge - 5 Problems
Splat Expression Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ service_behavior
intermediate2: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 }
Attempts:
2 left
💡 Hint
The
public_ip attribute is only set after the instance is created and if it has a public IP assigned.✗ Incorrect
The splat expression
aws_instance.example[*].public_ip collects the public_ip attribute from all instances. Since these instances do not have an associated public IP by default, the output is a list of nulls.❓ Configuration
intermediate2: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 = ??? }
Attempts:
2 left
💡 Hint
The splat expression syntax is
resource_name[*].attribute to get a list of attribute values.✗ Incorrect
Option C correctly uses the splat expression to get a list of all subnet IDs from the
aws_subnet.example resources. Other options either have incorrect syntax or reference non-existent attributes.❓ Architecture
advanced2: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 = ??? }
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.
✗ Incorrect
Since the module is called once, its output
private_ips is already a list of IPs. Using splat expressions like module.web_servers[*].private_ips is invalid because the module is not a list.❓ security
advanced2: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 = ??? }
Attempts:
2 left
💡 Hint
Check the correct syntax for splat expressions on resource attributes.
✗ Incorrect
Option A is invalid because
aws_instance.app.private_ip is not a list, so applying [*] causes a syntax error. The correct splat expression is aws_instance.app[*].private_ip.🧠 Conceptual
expert3: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 }
Attempts:
2 left
💡 Hint
Nested splat expressions produce nested lists reflecting the structure of the nested blocks.
✗ Incorrect
The expression
aws_lb.example[*].subnet_mapping[*].allocation_id returns a list of lists because subnet_mapping is a nested block list inside each load balancer resource. Since there is one load balancer, the outer list has one element which is a list of allocation IDs.