0
0
Terraformcloud~5 mins

Type conversion functions in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you need to change the type of a value in Terraform to make it work with other parts of your configuration. Type conversion functions help you turn one type of data into another, like changing a number into a string or a list into a set.
When you want to join numbers and text together in a message.
When you need to convert a list of strings into a set to remove duplicates.
When you want to convert a string that looks like a number into an actual number to do math.
When you need to change a map into a list to loop over its values.
When you want to make sure a value is treated as a boolean true or false.
Config File - main.tf
main.tf
variable "number_var" {
  type    = number
  default = 42
}

variable "string_var" {
  type    = string
  default = "100"
}

variable "list_var" {
  type    = list(string)
  default = ["apple", "banana", "apple"]
}

output "number_to_string" {
  value = tostring(var.number_var)
}

output "string_to_number" {
  value = tonumber(var.string_var)
}

output "list_to_set" {
  value = toset(var.list_var)
}

output "string_to_bool" {
  value = tobool("true")
}

This Terraform file shows examples of type conversion functions:

  • tostring() converts a number to a string.
  • tonumber() converts a string to a number.
  • toset() converts a list to a set, removing duplicates.
  • tobool() converts a string to a boolean.

These outputs help you see the converted values after applying the configuration.

Commands
This command sets up Terraform in the current folder by downloading necessary plugins and preparing the environment.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/random... - Installing hashicorp/random v3.4.3... - Installed hashicorp/random v3.4.3 (signed by HashiCorp) Terraform has been successfully initialized!
This command applies the Terraform configuration, creating outputs that show the results of type conversions.
Terminal
terraform apply -auto-approve
Expected OutputExpected
Apply complete! Resources: 0 added, 0 changed, 0 destroyed. Outputs: list_to_set = [ "apple", "banana", ] number_to_string = "42" string_to_bool = true string_to_number = 100
-auto-approve - Automatically approve the apply without asking for confirmation
This command shows the current output values from the last apply, letting you verify the converted types.
Terminal
terraform output
Expected OutputExpected
number_to_string = "42" string_to_number = 100 list_to_set = [ "apple", "banana", ] string_to_bool = true
Key Concept

If you remember nothing else from this pattern, remember: Terraform's type conversion functions let you safely change data types so your configuration works smoothly.

Common Mistakes
Trying to convert a string that is not a valid number using tonumber()
Terraform will return an error because it cannot turn invalid text into a number.
Make sure the string contains only digits or a valid number format before using tonumber().
Using tobool() on strings other than "true" or "false"
Terraform only accepts "true" or "false" (case insensitive) for boolean conversion; other values cause errors.
Use only "true" or "false" strings with tobool(), or use boolean variables directly.
Assuming toset() keeps the order of elements from the list
Sets do not preserve order; the output order may change and duplicates are removed.
Use toset() only when order does not matter and you want to remove duplicates.
Summary
Use tostring(), tonumber(), toset(), and tobool() to convert values between types in Terraform.
Run terraform init to prepare your environment before applying changes.
Use terraform apply to see the converted values and terraform output to check them anytime.