Skip to main content

Using String Functions

1. Using chomp and trim Functions

Function Description Example Result
chomp Removes trailing newline characters (\n) from a string. chomp("hello\n") "hello"
trim Removes leading and trailing whitespace (spaces, tabs, newlines) from a string. trim(" hello world \n\t") "hello world"

Example: Using chomp and trim in a Terraform template

resource "local_file" "example" {
  content = chomp(trim(file("input.txt")))
  filename = "output.txt"
}

2. Using format and formatlist Functions

Function Syntax Description Example
format format(formatstr, args...) Formats a string using a format string and arguments format("Hello %s, you are %d years old", "Alice", 30)
formatlist formatlist(formatstr, args...) Formats a list of strings using a format string and arguments formatlist("User %s has age %d", ["Bob", "Charlie"], [25, 35])

Example: Using format Function

resource "aws_instance" "web" {
  ami           = "ami-12345678"
  instance_type = format("t3.%s", var.instance_size)
}

Example: Using formatlist Function

output "instance_names" {
  value = formatlist("web-server-%d", range(1, 4))
}

3. Using join and split Functions

Function Signature Description Example
join join(separator, list) Joins elements of a list into a single string using the specified separator. join(",", [1, 2, 3]) // "1,2,3"
split split(separator, string) Splits a string into a list of substrings based on the specified separator. split(",", "a,b,c") // ["a", "b", "c"]

Example: join Function Usage

resource "aws_instance" "web" {
  ami           = "ami-12345678"
  instance_type = join("-", ["t2", "micro"])
}

Example: split Function Usage

variable "tags" {
  type = list(string)
  default = ["env:prod", "team:backend"]
}

locals {
  tag_pairs = split(":", join(",", var.tags))
}

4. Using lower and upper Functions

Function Description Example Result
lower() Converts a string to lowercase "HELLO WORLD" "hello world"
upper() Converts a string to uppercase "hello world" "HELLO WORLD"

Example: Using lower() and upper() Functions

resource "aws_s3_bucket" "example" {
  bucket = lower("EXAMPLE-BUCKET")
}

resource "aws_s3_bucket" "example_upper" {
  bucket = upper("example-bucket")
}

5. Using replace Function

Function Syntax Description Example
replace replace(string, search, replace) Replaces all occurrences of a substring with another substring. replace("hello world", "world", "Terraform")

Example: Replace in a variable

variable "greeting" {
  type = string
  default = "Hello world"
}

output "modified_greeting" {
  value = replace(var.greeting, "world", "Terraform")
}
Note: The replace function is case-sensitive.

6. Using substr Function

Function Signature Description Example
substr substr(string, start, length) Returns a substring of the input string starting at start index with optional length. substr("Hello World", 6, 5) // "World"

Example: Extracting Substring

output "substring" {
  value = substr("Terraform is powerful", 0, 9)
}

Example: Using with Variables

variable "input_string" {
  type = string
  default = "terraform-0.15.0"
}

output "version_part" {
  value = substr(var.input_string, 11, 5)
}

7. Using regex and regexall Functions

Function Description Signature Example
regex Extracts the first match of a regular expression from a string. regex(pattern, string) Example: Extracting a version number version = regex("v(\\d+\\.\\d+\\.\\d+)", "Release v1.2.3 is now available")
regexall Extracts all matches of a regular expression from a string and returns them as a list. regexall(pattern, string) Example: Finding all IP addresses ips = regexall("\\b(?:[0-9]{1,3}\\.){3}[0-9]{1,3}\\b", "Server IPs: 192.168.1.1, 10.0.0.1")
Note: The regex function returns the first match as a string, while regexall returns all matches as a list of strings.

8. Using startswith and endswith Functions

Function Description Signature Example
startswith Returns true if a string starts with the specified prefix. startswith(string, prefix) startswith("hello world", "hello") // true
endswith Returns true if a string ends with the specified suffix. endswith(string, suffix) endswith("hello world", "world") // true
Note: These functions are case-sensitive.

9. Using strrev Function

Function Signature Description Example
strrev strrev(string) Reverses the order of characters in a string. strrev("hello") // Returns "olleh"

Example: Reversing a String in Terraform

output "reversed_string" {
  value = strrev("terraform")
}

10. Using title Function

Function Signature Description Example
title title(string) Converts a string to title case (first letter of each word capitalized). title("hello world")

Example: title Function Usage

title("hello world") // Returns "Hello World"
title("terraform is awesome") // Returns "Terraform Is Awesome"

Was this article helpful?