Terraform
convert function
convert evaluates an expression from the first argument, and converts the
resulting value to the type constraint from the second argument. If the conversion
is not possible, an error is returned. The
type constraint argument uses
convert evaluates an expression from the first argument, and converts the
resulting value to the type constraint from the second argument. If the conversion
is not possible, Terraform returns an error. The type constraint argument uses
the same type expressions as variable and output type parameters.
Refer to the type constraint documentation for more information.
Examples
convert([], list(string))
// converts the empty tuple value to a list(string)
> convert([], list(string))
tolist([])
> convert({test = {}}, map(object({
attr = optional(string)
with_default = optional(string, "default")
})))
tomap({
"test" = {
"attr" = tostring(null)
"with_default" = "default"
}
})
The existing type conversion functions will remain for compatibility, and they
function identically to the corresponding convert invocations. For collection
types, we recommend you use convert to specify a more precise type to
avoid the more ambiguous type inference behavior of any.
tostring(v) => convert(v, string)
tonumber(v) => convert(v, number)
tobool(v) => convert(v, bool)
toset(v) => convert(v, set(any))
tolist(v) => convert(v, list(any))
tomap(v) => convert(v, map(any))