All technological notes.
Built-in function
Syntax
${name(arg1, arg2, ...)}Example
${file("mykey.pub")}: read the file contentdoc:
can use terraform console to test function
terraform console| Function | Description |
|---|---|
abs(number) |
Returns the absolute value. |
ceil(number) |
Rounds a number up to the nearest whole number. |
floor(number) |
Rounds a number down to the nearest whole number. |
max(a, b, …) |
Returns the largest value from the given arguments. |
min(a, b, …) |
Returns the smallest value from the given arguments. |
| Function | Description |
|---|---|
lower(string) |
Converts to lowercase. |
upper(string) |
Converts to uppercase. |
trimspace(string) |
Removes leading and trailing whitespace. |
replace(target_str, sub_str, replace_str) |
Replaces substring with another string. |
split(delimiter, string) |
Splits a string into a list using a delimiter. |
join(delimiter, list) |
Joins a list of strings into one string using a delimiter. |
format(format, values…) |
Formats a string using placeholders like printf. |
coalesce(str1, str2, ...) |
returns the first one that isn’t null or an empty string. |
substr(str, offset, length) |
Extract substring from string |
| Function | Description |
|---|---|
length(list/map/string) |
Returns the number of elements in a list/map or characters in a string. |
contains(list, value) |
Checks if a list contains a given value. |
element(list, index) |
Returns the element at a given index (negative index counts from end). |
lookup(map, key, default) |
Gets a value from a map, or returns a default if missing. |
keys(map) |
Returns a list of keys from a map. |
values(map) |
Returns a list of values from a map. |
coalesce(list1, list2, ...) |
returns the first one that isn’t null. |
index(list,element) |
returns the index of a given element in a list |
list(item1, item2) |
Create a new list |
map(key1, val1, key2, val2) |
Create a map |
merge(map1, map2, …) |
Combines multiple maps into one. |
slice(list, start_index,length) |
Slice a list |
Example:
lookup(map("k", "v"), "k", "Not found"): returns “v”File & Encoding
| Function | Description |
|---|---|
basename(path) |
Get the filename (only the filename in a path) |
file(path) |
Reads the contents of a file. |
filebase64(path) |
Reads a file and returns Base64-encoded text. |
base64encode(string) |
Encodes a string to Base64. |
base64decode(string) |
Decodes a Base64 string. |
| Function | Description |
|---|---|
timestamp() |
Returns the current UTC timestamp in RFC 3339 format. |
timeadd(timestamp, duration) |
Adds a duration (e.g., "1h", "30m") to a timestamp. |
| Function | Description |
|---|---|
cidrsubnet(prefix, newbits, netnum) |
Calculates a subnet address within a CIDR range. |
cidrhost(prefix, hostnum) |
Returns a specific host IP address within a CIDR range. |
| Function | Description |
|---|---|
uuid() |
Generates a random UUID each time it’s evaluated. |
terraform console"hello world"
# "hello world"
replace("hello world", "o", "@")
# "hell@ w@rld"
"the server launched at ${timestamp()}"
# "the server launched at 2025-08-14T16:34:12Z"
tolist(["subnet-1","subnet-2","subnet-3"])
# tolist([
# "subnet-1",
# "subnet-2",
# "subnet-3",
# ])
split(",","subnet-1,subnet-2,subnet-3")
# tolist([
# "subnet-1",
# "subnet-2",
# "subnet-3",
# ])
element(tolist(["subnet-1","subnet-2","subnet-3"]),0)
# "subnet-1"
element(tolist(["subnet-1","subnet-2","subnet-3"]),3)
# "subnet-1"
element(tolist(["subnet-1","subnet-2","subnet-3"]),4)
# "subnet-2"
element(tolist(["subnet-1","subnet-2","subnet-3"]),5)
# "subnet-3"
index(tolist(["subnet-1","subnet-2","subnet-3"]),"subnet-1")
# 0
slice(tolist(["subnet-1","subnet-2","subnet-3"]),0,2)
# tolist([
# "subnet-1",
# "subnet-2",
# ])
join(":", tolist(["subnet-1","subnet-2","subnet-3"]))
# "subnet-1:subnet-2:subnet-3"
tomap({"us-east-1"="ami-1", "ca-central-1"="ami-2"})
# tomap({
# "ca-central-1" = "ami-2"
# "us-east-1" = "ami-1"
# })
lookup(tomap({"us-east-1"="ami-1", "ca-central-1"="ami-2"}), "ca-central-1")
# "ami-2"
substr("abcd", 0, 1)
# "a"
substr("abcd", 0, 4)
# "abcd"
substr("abcd", 0, 2)
# "ab"