Terraform

How to Use Terraform Toset Function [Examples]

How to Use Terraform Toset Function [Examples]

In this article, we will take a look at the type conversion function in Terraform, toset() , explaining what it can be used for, the difference between it and the tolist() function, along with some useful examples showing how to use it within a for_each loop.

What is the Terraform toset function?

Terraform toset is a type conversion function. It can be used to convert supplied values to a set, ensuring uniqueness and unordered nature. The syntax of this function is toset(value), where value is the input value you want to convert to a set.

In Terraform, a set is a collection type that represents an unordered collection of unique values. Sets are used to store and manage distinct elements, and they are commonly used for scenarios where uniqueness and order are not important. The set data type is useful when you want to ensure that each element is unique within the collection.

What is the difference between toset and tolist?

The toset() function converts a value to a set, that is an unordered collection of unique values, while tolist() converts a value to a list, preserving the order of elements.

A list in Terraform is a collection type that represents an ordered sequence of elements. Lists are used to store and manage a series of values, and the order in which elements are defined in the list is maintained. Elements in a list can be of various types, such as strings, numbers, or other complex data structures.

Read more about other Terraform functions.

How to use Terraform toset — example

In the example below, we have a list we convert into a set.

variable "my_list" {
  type    = list(string)
  default = ["luke", "yoda", "darth"]
}

locals {
  my_set = toset(var.my_list)
}

output "set_output" {
  value = local.my_set
}

The output would show:

Outputs:

set_output = toset([
  "luke",
  "luke",
  "darth",
])

You can also use the Terraform console to experiment with Terraform functions. For example, the simple test supplying the values directly to the toset()function on the terraform console shows the same output:

toset functions

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 (for example, if strings and numbers are supplied in the list together). Also note that sets cannot contain duplicate values, and any duplicate values supplied are coalesced. Sets are also unordered, so any ordering when supplying the list is also lost.

For example:

toset in terraform

Terraform for_each with toset

The for_each construct in Terraform is often used to iterate over a map or a set of values and create multiple instances of a resource or module. If you have a set of values and you want to use for_each in conjunction with toset(), you’ll need to convert your input data to a set.

variable "my_list" {
  type    = list(string)
  default = ["luke", "yoda", "darth"]
}

resource "example_resource" "my_resource" {
  for_each = toset(var.my_list)

  name = each.value
}

output "character_names" {
  value = example_resource.my_resource[*].name
}

On running terraform apply, the output will show:

Outputs:

character_names = [
  "luke",
  "yoda",
  "darth",
]

Key points

In Terraform, type conversion functions are used to convert values from one data type to another. These functions allow you to handle and manipulate different types of data in your Terraform configurations. The toset() function allows you to convert supplied values to a set (ensuring uniqueness and unordered nature).

We encourage you also to explore how Spacelift makes it easy to work with Terraform.

If you need any help managing your Terraform infrastructure, building more complex workflows based on Terraform, and managing AWS credentials per run, instead of using a static pair on your local machine, Spacelift is a fantastic tool for this. It supports Git workflows, policy as code, programmatic configuration, context sharing, drift detection, and many more great features right out of the box.

You can check it for free, by creating a trial account.

Note: New versions of Terraform are placed under the BUSL license, but everything created before version 1.5.x stays open-source. OpenTofu is an open-source version of Terraform that expands on Terraform’s existing concepts and offerings. It is a viable alternative to HashiCorp’s Terraform, being forked from Terraform version 1.5.6.

Automate Terraform Deployments with Spacelift

Automate your infrastructure provisioning, and build more complex workflows based on Terraform using policy as code, programmatic configuration, context sharing, drift detection, resource visualization, and many more.

Start free trial

The Practitioner’s Guide to Scaling Infrastructure as Code

Transform your IaC management to scale

securely, efficiently, and productively

into the future.

ebook global banner
Share your data and download the guide