Function join()
📝 Description
The join function produces a string by concatenating together all elements of a list, using a specific separator between each element.
Use Case: Creating comma-separated lists for Security Group rules, building environment paths, or generating human-readable labels from a list of strings.
💻 Syntax
💡 Examples
1. Creating a Comma-Separated List:
Useful for policy documents or metadata.
# Result: "admin, developer, guest"
output "users" {
value = join(", ", ["admin", "developer", "guest"])
}
2. Building a Custom Path:
3. Practical Scenario: Security Group Rules
Imagine you have a list of CIDR blocks and you need to pass them as a single string (though many resources accept lists directly, some legacy modules or scripts might require a string):
variable "whitelist_ips" {
default = ["10.0.1.0/24", "1.1.1.1/32", "8.8.8.8/32"]
}
locals {
# Result: "10.0.1.0/24,1.1.1.1/32,8.8.8.8/32"
authorized_ranges = join(",", var.whitelist_ips)
}
⚠️ Common Errors
| Input | Result | Why? |
|---|---|---|
join(",", "string") |
Error | Second argument must be a list, not a single string. |
join(",", [1, 2]) |
Success | Terraform will automatically convert numbers to strings inside the list. |
🔗 Related Functions
-
[[split]] — The opposite: turns a string into a list.
-
[[format]] — Better for complex templates where logic is more than just "glueing".