convert Function
convert performs an explicit type conversion to make the given value conform
to the given target type constraint.
convert(value, type)
A value of any type can be provided as long as it can be converted to match the given type constraint.
Explicit type conversions are rarely necessary in OpenTofu because it converts
types automatically where required. This function can be useful in situations
where the type of a result is ambiguous, such as when parsing an arbitrary JSON
string using jsonencode and then ensuring the resulting
data structure conforms to the expected type.
The type argument expects a type constraint using the same syntax as for input variables, as described in Type Constraints. The type constraint may include object types with optional attributes, but the default values for any optional attributes must be constant values and cannot use functions or references to other symbols in the module.
Examples​
> convert("true", bool)
true
> convert({}, object({ name = optional(string, "OpenTofu") }))
{
name = "OpenTofu"
}
Related Functions​
toboolis a shorthand forconvertwith a type constraint ofbool.tolistis a shorthand forconvertwith a type constraint oflist(any).tomapis a shorthand forconvertwith a type constraint ofmap(any).tonumberis a shorthand forconvertwith a type constraint ofnumber.tosetis a shorthand forconvertwith a type constraint ofset(any).tostringis a shorthand forconvertwith a type constraint ofstring.