You want to output the private IPs of all EC2 instances created with `for_each`. Which HCL expression produces a map of `instance_name => private_ip`?

Direct Answer (Verified)Reviewed by Sunny (Senior Cloud Engineer)

Correct Answer: `{ for k, v in aws_instance.web : k => v.private_ip }`

When `for_each` is used, the resource is a map keyed by the `for_each` keys. `{ for k, v in aws_instance.web : k => v.private_ip }` iterates that map and produces a new map of inst...

Real Exam Scenario

You want to output the private IPs of all EC2 instances created with `for_each`. Which HCL expression produces a map of `instance_name => private_ip`?

✓ Correct Answer & Explanation

Correct Answer

`{ for k, v in aws_instance.web : k => v.private_ip }`

Why This Answer Is Correct

When `for_each` is used, the resource is a map keyed by the `for_each` keys. `{ for k, v in aws_instance.web : k => v.private_ip }` iterates that map and produces a new map of instance name to private IP. The splat expression `*.private_ip` only works with `count`-indexed resources (list form), not `for_each` resources (map form).