Skip to content

Function split()

📝 Description

The split function produces a list of strings by dividing a single string at each occurrence of a specific separator.

Use Case: Extracting data from structured strings like CSV lines, file paths, or complex cloud identifiers (ARNs/Resource IDs).


💻 Syntax

split(separator, string)

💡 Examples

1. Creating a List from a CSV-like string:

# Result: ["admin", "developer", "guest"]
output "user_list" {
  value = split(",", "admin,developer,guest")
}

2. Extracting a specific part of a string:

Imagine you have an email address and you only want the domain. You split by @ and take the second element (index 1).

variable "email" { default = "[email protected]" }

locals {
  # Result: "example.com"
  domain = split("@", var.email)[1]
}

3. Parsing a Path:

# Result: ["", "usr", "local", "bin"]
output "path_parts" {
  value = split("/", "/usr/local/bin")
}

⚠️ Key Details

  • If the separator is an empty string (""), the function will return a list containing the original string as its only element (it won't split into individual characters).

  • If the separator is not found in the string, the result is a list with one element: the original string.


  • [[join]] — The opposite: turns a list into a string.

  • [[replace]] — Better if you just want to change characters without creating a list.

  • [[element]] — Use this to safely pick an item from the list produced by split.

  • Official HashiCorp Docs