2019-01-17 18:11:48 +01:00
|
|
|
---
|
2021-12-15 03:41:17 +01:00
|
|
|
page_title: toset - Functions - Configuration Language
|
|
|
|
description: The toset function converts a value to a set.
|
2019-01-17 18:11:48 +01:00
|
|
|
---
|
|
|
|
|
|
|
|
# `toset` Function
|
|
|
|
|
|
|
|
`toset` converts its argument to a set value.
|
|
|
|
|
|
|
|
Explicit type conversions are rarely necessary in Terraform because it will
|
|
|
|
convert types automatically where required. Use the explicit type conversion
|
|
|
|
functions only to normalize types returned in module outputs.
|
|
|
|
|
|
|
|
Pass a _list_ value to `toset` to convert it to a set, which will remove any
|
|
|
|
duplicate elements and discard the ordering of the elements.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
```
|
|
|
|
> toset(["a", "b", "c"])
|
|
|
|
[
|
|
|
|
"a",
|
|
|
|
"b",
|
|
|
|
"c",
|
|
|
|
]
|
|
|
|
```
|
|
|
|
|
|
|
|
Since Terraform's concept of a set requires all of the elements to be of the
|
|
|
|
same type, mixed-typed elements will be converted to the most general type:
|
|
|
|
|
|
|
|
```
|
2019-03-11 21:59:12 +01:00
|
|
|
> toset(["a", "b", 3])
|
2019-01-17 18:11:48 +01:00
|
|
|
[
|
|
|
|
"a",
|
|
|
|
"b",
|
|
|
|
"3",
|
|
|
|
]
|
|
|
|
```
|
|
|
|
|
|
|
|
Set collections are unordered and cannot contain duplicate values, so the
|
|
|
|
ordering of the argument elements is lost and any duplicate values are
|
|
|
|
coalesced:
|
|
|
|
|
|
|
|
```
|
2019-03-11 21:59:12 +01:00
|
|
|
> toset(["c", "b", "b"])
|
2019-01-17 18:11:48 +01:00
|
|
|
[
|
|
|
|
"b",
|
|
|
|
"c",
|
|
|
|
]
|
|
|
|
```
|