Terraform Functions

The Terraform language includes a number of built-in functions that you can call from within expressions to transform and combine values. The general syntax for function calls is a function name followed by comma-separated arguments in parentheses

max(5, 12, 9)

The Terraform language does not support user-defined functions, and so only the functions built into the language are available for use.

Numeric Functions

abs

abs returns the absolute value of the given number. In other words, if the number is zero or positive then it is returned as-is, but if it is negative then it is multiplied by -1 to make it positive before returning it.

> abs(24)
23
> abs(0)
0
> abs(-24.4)
24.4

ceil

ceil returns the closest whole number that is greater than or equal to the given value, which may be a fraction.

> ceil(5)
5
> ceil(5.1)
6

floor

floor returns the closest whole number that is less than or equal to the given value, which may be a fraction.

> floor(5)
5
> floor(4.9)
4

log

log returns the logarithm of a given number in a given base.

> log(50, 10)
1.6989700043360185
> log(16, 2)
4

max

max takes one or more numbers and returns the greatest number from the set.

> max(12, 54, 3)
54

min

min takes one or more numbers and returns the smallest number from the set.

> min(12, 54, 3)
3

pow

pow calculates an exponent, by raising its first argument to the power of the second argument.

> pow(3, 2)
9
> pow(4, 0)
1

String Functions

join

join produces a string by concatenating together all elements of a given list of strings with the given delimiter.

> join(", ", ["foo", "bar", "baz"])
foo, bar, baz
> join(", ", ["foo"])
foo

upper

upper converts all cased letters in the given string to uppercase.

> upper("hello")
HELLO

lower

lower converts all cased letters in the given string to lowercase.

> lower("HELLO")
hello

title

title converts the first letter of each word in the given string to uppercase.

> title("hello world")
Hello World

lets take an example of String functions

create a file variable.tf with below code:

variable users {
    type = list
    default = ["gaurav","Saurav","anKit"]
}

create a file first.tf (you can change the file name as per your convenient.

output printfirst {
        value = "${join("--->",var.users)}"
}
output helloworldupper {
        value = "${upper(var.users[0])}"
}
output helloworldlower {
        value = "${lower(var.users[1])}"
}
output helloworldtitle {
        value = "${title(var.users[2])}"
}

let's run terraform apply and see the output:

└─$ terraform plan
No changes. Your infrastructure matches the configuration.
Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:
helloworldlower = "saurav"
helloworldtitle = "AnKit"
helloworldupper = "GAURAV"
printfirst = "gaurav--->Saurav--->anKit"
┌──(gaurav㉿learning-ocean)-[~/terraform/youtube-course/functions]
└─$

Below are the types of other functions with function names:

  • Numeric Functions : abs, ceil, floor, log, max, min, parseint, pow, signum
  • String Functions: chomp, format, formalist, join, lower, regex, regexall, rep[lace, split, strrev, title, trim, trimprefix, trimsuffix, trimspace, upper
  • Collection Functions: alltrue, anytrue, chunklist, coalesce, coalescelist, compact, concat, contains, distinct, element, flatten, index, keys, length, list, lookup, map, matchkeys,merge, one, range, reverse, setintersection, setproduct, setsubsctract, setunion, slice, sort, sum, transpose, values, zipmap
  • Encoding Functions: base643ncode, base64decode, base64gzip, csvdecode, jsonencode, jsondecode, urlencode, yamlencode, yamldecode
  • Filesystem functions: absath, dirname, pathexpand, basename, file, fileexists, fileset, filebase64, templatefile
  • Date & Time Functions: formade, timeadd, timestamp
  • Hash and Crypto Functions : base64sha256, base64sha512, bcrypt, filebase64sha512, filemd5, filesha1, filesha256, filesha512, md5, rsadecrypt, sha, sha256, sha512, uuid, uuidv5
  • IP Network Functions: cidrhost, cidrnetmask, cidrsubnets
  • Type Conversion functions: can, defaults, nonsensative, sensitive, tobool, tolist, tomap, tonumber, toset, tostring, try

Click Here for Reference Docs

Demo Video