Type Constraints - Set
Learn how to use the Terraform type constraints Set.
We'll cover the following
Set#
A set is almost the same as a list. The key difference is that a set only contains unique values.
In the above example, we define a variable called my_set
and initialize it to the set [7, 2, 2]
. As stated above, a set only contains unique values,
so when we run this project we will define/specify that the output value set will print [7, 2]
. Terraform removes one of the 2
values as it was a duplicate.
To show how sets can be useful, we define a list called my_list
to repeat the value foo
twice. The value of the list output will be ["foo", "bar", "foo"]
because we are outputting the value of my_list
, which is of type list and lists can contain duplicate values.
For the output list_as_set
, we are using the toset
function to convert the my_list
variable to a set. The value of this output will be ["foo", "bar"]
. Because we are converting the list to a set, Terraform removes the duplicate value "foo"
.
Running the project#
Clicking the terminal will run terraform init
and terraform apply
commands. When prompted, type yes
and press enter.
📝Note: Once you are done with the project, do not forget to run
terraform destroy
and then confirm with yes to delete all of the resources in this project.