A local is defined as admins = { for name, role in var.users : name => role if role == "admin" }, where var.users is {alice = "admin", bob = "dev", carol = "admin"}. What is the value of local.admins?
Select an answer to reveal the explanation.
Short Explanation
The brackets around a for expression tell you the shape of the box you'll get: braces mean key-value pairs, square brackets mean a list. The if clause is a filter at the door, so bob simply never gets in. The trap is thinking the filter changes the shape or leaves a hole behind.
Full Explanation
A for expression's surrounding delimiters determine its result type. Square brackets produce a tuple, and braces with a key => value pair produce an object, which Terraform converts to a map wherever a map is expected. The optional if clause filters the input elements: any element for which the condition is false is excluded entirely from the result. Here only alice and carol satisfy role == "admin", so local.admins has those two keys, each with the value admin, and bob does not appear at all. The if clause never changes the result type; a filtered brace expression is still a key-value result, and producing a list of keys would require square brackets and an expression such as name. Filtering does not leave placeholders, so no key maps to null for a failed condition. if clauses are valid in both list-building and map-building for expressions, so there is no error. Exam caveat: when a brace for expression would produce duplicate keys, Terraform errors unless the grouping operator, an ellipsis after the value, is used to collect values into lists. Operational check: evaluate local.admins in terraform console and confirm the keys, then evaluate keys(local.admins) for the list form if that is what a consumer needs.