0
0
Terraformcloud~5 mins

String functions (join, split, format) in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
Terraform string functions help you change and combine text easily. They solve problems like joining words with separators, splitting text into parts, and inserting values into templates.
When you want to combine a list of server names into a single comma-separated string for configuration.
When you receive a string of IP addresses separated by spaces and need to split them into a list for processing.
When you want to create a formatted message that includes variable values like region or environment.
When you need to build resource names dynamically by joining parts with dashes.
When you want to parse a string input into components to use separately in your infrastructure code.
Config File - main.tf
main.tf
variable "server_names" {
  type    = list(string)
  default = ["app1", "app2", "app3"]
}

variable "ip_string" {
  type    = string
  default = "192.168.1.1 192.168.1.2 192.168.1.3"
}

output "joined_servers" {
  value = join(",", var.server_names)
}

output "split_ips" {
  value = split(" ", var.ip_string)
}

output "formatted_message" {
  value = format("Servers: %s are running in %s environment.", join(", ", var.server_names), "production")
}

variable "server_names" defines a list of server names.

variable "ip_string" holds a space-separated string of IP addresses.

output "joined_servers" uses join to combine server names with commas.

output "split_ips" uses split to turn the IP string into a list.

output "formatted_message" uses format to create a message inserting joined server names and environment.

Commands
Initialize the Terraform working directory to download necessary plugins and prepare for execution.
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!
Apply the Terraform configuration to see the outputs of string functions without manual approval.
Terminal
terraform apply -auto-approve
Expected OutputExpected
Apply complete! Resources: 0 added, 0 changed, 0 destroyed. Outputs: formatted_message = "Servers: app1, app2, app3 are running in production environment." joined_servers = "app1,app2,app3" split_ips = [ "192.168.1.1", "192.168.1.2", "192.168.1.3", ]
-auto-approve - Skip manual approval to apply changes immediately
Display the output value that shows the joined server names as a single string.
Terminal
terraform output joined_servers
Expected OutputExpected
app1,app2,app3
Display the output value that shows the list of IP addresses after splitting the string.
Terminal
terraform output split_ips
Expected OutputExpected
["192.168.1.1", "192.168.1.2", "192.168.1.3"]
Key Concept

If you remember nothing else from this pattern, remember: join combines lists into strings, split breaks strings into lists, and format inserts values into text templates.

Common Mistakes
Using join with a separator but passing a string instead of a list.
join expects a list as the second argument; passing a string causes an error.
Always pass a list of strings to join, like join(",", var.server_names).
Using split with a separator that does not exist in the string.
split returns the whole string as a single element list if the separator is not found, which may not be expected.
Ensure the separator matches the string format, for example split(" ", var.ip_string) if IPs are space-separated.
Using format placeholders incorrectly, like missing %s or wrong number of arguments.
format requires placeholders to match the number of arguments; mismatch causes errors.
Count placeholders and arguments carefully, e.g., format("Hello %s", "world").
Summary
Use terraform init to prepare your working directory before applying configurations.
Use join to combine lists of strings into one string with separators.
Use split to break a string into a list based on a separator.
Use format to create strings with inserted variable values.
Use terraform output commands to see the results of your string functions.