0
0
Terraformcloud~5 mins

Type conversion behavior in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
Terraform uses type conversion to change values from one type to another automatically or explicitly. This helps avoid errors when combining different data types in your infrastructure code.
When you want to combine a number and a string in a resource argument.
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 for calculations.
When you want to ensure a variable is treated as a specific type to avoid errors.
When you want to convert a map to a list or vice versa for easier data handling.
Config File - main.tf
main.tf
variable "input_string" {
  type    = string
  default = "123"
}

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

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

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

output "number_to_string" {
  value = tostring(456)
}

This Terraform file defines two variables: a string and a list of strings. It shows three outputs demonstrating type conversion:

  • tonumber() converts a string to a number.
  • toset() converts a list to a set, removing duplicates.
  • tostring() converts a number to a string.
Commands
Initializes the Terraform working directory and downloads necessary providers.
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! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure.
Applies the Terraform configuration, creating outputs that show type conversions in action.
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 = "456" string_to_number = 123
-auto-approve - Automatically approves the apply step without prompting.
Shows the output value of the string converted to a number.
Terminal
terraform output string_to_number
Expected OutputExpected
123
Shows the output value of the list converted to a set, duplicates removed.
Terminal
terraform output list_to_set
Expected OutputExpected
["apple", "banana"]
Key Concept

If you remember nothing else from this pattern, remember: Terraform functions like tonumber(), tostring(), and toset() let you safely convert data types to avoid errors and handle data correctly.

Common Mistakes
Trying to use a string as a number without converting it first.
Terraform will throw a type error because it expects a number but got a string.
Use the tonumber() function to convert the string to a number before using it in calculations.
Assuming lists automatically remove duplicates without conversion.
Lists can contain duplicates; Terraform treats them as separate items.
Use toset() to convert a list to a set, which removes duplicates.
Summary
Use Terraform's built-in functions like tonumber(), tostring(), and toset() to convert data types.
Initialize Terraform with 'terraform init' before applying configurations.
Apply the configuration with 'terraform apply' to see type conversions in outputs.