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
💡 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 = "ivan@example.com" }
locals {
# Result: "example.com"
domain = split("@", var.email)[1]
}
3. Parsing a Path:
⚠️ 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.