In Terraform 1.15, a module decodes JSON into local.raw and needs it as exactly list(object({ name = string, port = optional(number, 443) })), with the port default applied to entries that omit it, instead of relying on type inference. Which expression does that?
Select an answer to reveal the explanation.
Short Explanation
Think of convert() as handing Terraform the exact mould you want the value poured into, defaults and all. tolist() just says make it some kind of list and lets Terraform guess the rest. The trap is assuming the old to-functions understand optional() defaults; they don't take a type at all.
Full Explanation
Terraform 1.15 added the convert function, which takes an expression and a type constraint written in the same syntax used for variable and output types, and converts the value to that type or returns an error if conversion is impossible. Because the constraint is an object type with optional attributes, conversion also fills in declared defaults, so any entry without port receives 443, and every element is checked to have a string name. The documentation recommends convert over the legacy functions for collections precisely because it states the target type instead of relying on inference with any. tolist() converts to a list but infers the element type from the data; it cannot apply optional defaults and does not verify the object's attributes. try() only returns the first expression that evaluates without error, so it would pass the raw value through unchanged and convert nothing. Mapping each element through tomap() turns the objects into maps, which forces all attribute values to a single type and still applies no defaults. Exam caveat: convert is new in 1.15, so a module that uses it should constrain required_version accordingly. Operational check: evaluate the convert expression in terraform console against sample data and confirm entries without port show 443.