Splat expressions and collection ordering
Extract one field from every record
A splat is a compact projection when you need the same field or indexed element from each member of a sequence. Consider two workers, each with a list of ports:
variable "nodes" { type = list(object({ name = string ports = list(number) })) default = [ { name = "worker-b", ports = [8081, 9091] }, { name = "worker-a", ports = [8080, 9090] } ]}
output "names" { value = var.nodes[*].name}
output "first_ports" { value = var.nodes[*].ports[0]}Predicted names: ["worker-b", "worker-a"]. Predicted first ports: [8081, 8080]. The [0] applies to each worker’s ports separately. The equivalent explicit transformation is [for node in var.nodes : node.ports[0]]. An empty nodes list produces empty results. If any worker has no ports, the first-ports expression fails; validate the input or design an explicit fallback rather than pretending a missing port is zero.
Make ordering an explicit design decision
Subnets are naturally identified by names, while a tag collection often needs only unique membership. This local example uses documentation-only synthetic private network ranges; it creates no network:
locals { subnets = { west = "10.20.2.0/24" east = "10.20.1.0/24" } tags = toset(["team-blue", "app", "team-blue"])}
output "subnet_ranges" { value = [for name, cidr in local.subnets : cidr]}
output "tag_names" { value = sort(tolist(local.tags))}Predicted subnet ranges: ["10.20.1.0/24", "10.20.2.0/24"]. Predicted tag names: ["app", "team-blue"]. The subnet transformation follows lexical key order, not declaration order or the numeric value of the CIDR. toset drops duplicate strings; sort gives a deliberate lexical string order for display. Lists and tuples keep their sequence. When a for expression converts a string set to an ordered result, values are visited lexically; other set element types do not provide a stable order contract. Do not use the order of a set of objects as an instance identity.
Adapt one optional object to a sequence
An optional monitoring configuration can be absent or describe one monitor. A modern splat can turn this particular non-collection value into a zero-or-one sequence:
variable "monitor" { type = object({ name = string }) default = null}
output "monitor_names" { value = var.monitor[*].name}With null input, the predicted result is []. With { name = "uptime" }, it is ["uptime"]. This can be useful when a downstream interface expects a collection. It does not mean that every object should be treated as a collection of its fields. For an inventory map, use an explicit for expression to visit its values. Prefer clear conditionals when the zero-or-one convention is not obvious.
Compare sequence and lookup operations
Try these independent console expressions:
[for name, port in { search = 8080, catalog = 8082 } : port][{ name = "west" }, { name = "east" }][*].name[for name in toset(["z", "a", "a"]) : name]Predicted results: [8082, 8080], ["west", "east"], and ["a", "z"]. A map of objects is not a sequence of its object values for splat purposes. Use an explicit for to access those records. Prefer modern [*] to legacy .*; the legacy syntax handles indexing after the attribute projection differently, which can select a different item or cause an error.
Practice predicting shape and order
First ask whether the input is a sequence, lookup, set, or optional singleton. Next decide whether the consumer needs a sequence with explicit ordering, unique membership, or keyed records. Finally test emptiness, repeated values and missing fields. A splat is concise for a simple projection; a for expression is usually clearer when filtering, constructing an object, or handling conditional cases. Continue to the built-in function library after completing the worksheet. All outputs in this chapter are static predictions; no Terraform execution has been performed.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Splat expressions and collection ordering — knowledge check
Check collection transformation and projection.
What does var.nodes[*].ports[0] return for the default workers?
What is the ordering of subnet_ranges in the example?
What does converting repeated tag strings to a set do?
What does monitor_names return when monitor is null?
What is the clearest way to project fields from a map of workers?