Hcl2 input variables, local variables and functions (#8588)
Mainly redefine or reused what Terraform did.
* allow to used `variables`, `variable` and `local` blocks
* import the following functions and their docs from Terraform: abs, abspath, basename, base64decode, base64encode, bcrypt, can, ceil, chomp, chunklist, cidrhost, cidrnetmask, cidrsubnet, cidrsubnets, coalesce, coalescelist, compact, concat, contains, convert, csvdecode, dirname, distinct, element, file, fileexists, fileset, flatten, floor, format, formatdate, formatlist, indent, index, join, jsondecode, jsonencode, keys, length, log, lookup, lower, max, md5, merge, min, parseint, pathexpand, pow, range, reverse, rsadecrypt, setintersection, setproduct, setunion, sha1, sha256, sha512, signum, slice, sort, split, strrev, substr, timestamp, timeadd, title, trim, trimprefix, trimspace, trimsuffix, try, upper, urlencode, uuidv4, uuidv5, values, yamldecode, yamlencode, zipmap.
2020-02-06 11:49:21 +01:00
|
|
|
---
|
2020-03-18 18:46:47 -04:00
|
|
|
page_title: convert - Functions - Configuration Language
|
|
|
|
description: 'The convert function converts a value or an expression to a given type. '
|
Hcl2 input variables, local variables and functions (#8588)
Mainly redefine or reused what Terraform did.
* allow to used `variables`, `variable` and `local` blocks
* import the following functions and their docs from Terraform: abs, abspath, basename, base64decode, base64encode, bcrypt, can, ceil, chomp, chunklist, cidrhost, cidrnetmask, cidrsubnet, cidrsubnets, coalesce, coalescelist, compact, concat, contains, convert, csvdecode, dirname, distinct, element, file, fileexists, fileset, flatten, floor, format, formatdate, formatlist, indent, index, join, jsondecode, jsonencode, keys, length, log, lookup, lower, max, md5, merge, min, parseint, pathexpand, pow, range, reverse, rsadecrypt, setintersection, setproduct, setunion, sha1, sha256, sha512, signum, slice, sort, split, strrev, substr, timestamp, timeadd, title, trim, trimprefix, trimspace, trimsuffix, try, upper, urlencode, uuidv4, uuidv5, values, yamldecode, yamlencode, zipmap.
2020-02-06 11:49:21 +01:00
|
|
|
---
|
|
|
|
|
|
|
|
# `convert` Function
|
|
|
|
|
|
|
|
`convert` converts a value or an expression to a given type.
|
|
|
|
|
|
|
|
Explicit type conversions are rarely necessary in HCL because it will convert
|
|
|
|
types automatically where required. Use the explicit type conversion functions
|
|
|
|
only to normalize types returned in outputs.
|
|
|
|
|
|
|
|
Only numbers and strings containing decimal representations of numbers can be
|
|
|
|
converted to number. All other values will produce an error.
|
|
|
|
|
|
|
|
Only boolean values and the exact strings "true" and "false" can be converted
|
|
|
|
to boolean. All other values will produce an error.
|
|
|
|
|
|
|
|
Only the primitive types (string, number, and bool) can be converted to string.
|
|
|
|
All other values will produce an error.
|
|
|
|
|
|
|
|
`convert(value, type_constraint)`
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2020-05-29 17:12:05 -04:00
|
|
|
```shell-session
|
Hcl2 input variables, local variables and functions (#8588)
Mainly redefine or reused what Terraform did.
* allow to used `variables`, `variable` and `local` blocks
* import the following functions and their docs from Terraform: abs, abspath, basename, base64decode, base64encode, bcrypt, can, ceil, chomp, chunklist, cidrhost, cidrnetmask, cidrsubnet, cidrsubnets, coalesce, coalescelist, compact, concat, contains, convert, csvdecode, dirname, distinct, element, file, fileexists, fileset, flatten, floor, format, formatdate, formatlist, indent, index, join, jsondecode, jsonencode, keys, length, log, lookup, lower, max, md5, merge, min, parseint, pathexpand, pow, range, reverse, rsadecrypt, setintersection, setproduct, setunion, sha1, sha256, sha512, signum, slice, sort, split, strrev, substr, timestamp, timeadd, title, trim, trimprefix, trimspace, trimsuffix, try, upper, urlencode, uuidv4, uuidv5, values, yamldecode, yamlencode, zipmap.
2020-02-06 11:49:21 +01:00
|
|
|
> convert(3, string)
|
|
|
|
"3"
|
|
|
|
> convert("3", number)
|
|
|
|
3
|
|
|
|
> convert("false", bool)
|
|
|
|
false
|
|
|
|
> convert(false, string)
|
|
|
|
"false"
|
|
|
|
> convert(["a", "b", 3], list)
|
|
|
|
[
|
|
|
|
"a",
|
|
|
|
"b",
|
|
|
|
"3",
|
|
|
|
]
|
|
|
|
> convert(["c", "b", "b"], set)
|
|
|
|
[
|
|
|
|
"b",
|
|
|
|
"c",
|
|
|
|
]
|
|
|
|
> convert({"a" = "foo", "b" = true}, map)
|
|
|
|
{
|
|
|
|
"a" = "foo"
|
|
|
|
"b" = "true"
|
|
|
|
}
|
|
|
|
```
|