DEV Community

Cover image for Terraform Functions
Sainath Patil
Sainath Patil

Posted on

Terraform Functions

Infrastructure as Code (IaC) must needed in modern DevOps practices, and Terraform is one of the most widely adopted IaC tools today.
Whether you're managing cloud resources or orchestrating multi-cloud environments, Terraform’s power lies not only in its declarative approach but also in its built-in functions.

What Are Terraform Functions?

Terraform functions are built-in helpers used to process and generate values inside your configuration. You cannot define your own functions, but Terraform provides a rich standard library covering:

  • String Functions

  • Numeric Functions

  • Collection Functions (lists, maps, sets)

  • Type conversions

  • File operations

  • Date/Time Functions

  • Validation Functions

  • Lookup Functions


1. String Functions

String functions help you format names, process input variables, and build dynamic resource identifiers.

  • lower()

Converts text to lowercase.

lower("MyBucketName")  # "mybucketname"
Enter fullscreen mode Exit fullscreen mode
  • upper()

Converts text to uppercase.

upper("dev-environment")  # "DEV-ENVIRONMENT"
Enter fullscreen mode Exit fullscreen mode
  • replace()

Replaces part of a string.

replace("app_v1", "v1", "v2")  # "app_v2"
Enter fullscreen mode Exit fullscreen mode
  • substr()

Gets a substring.

substr("terraform", 0, 4)  # "terr"
Enter fullscreen mode Exit fullscreen mode
  • trim()

Removes leading and trailing characters (default: whitespace).

trim("  hello  ")  # "hello"
Enter fullscreen mode Exit fullscreen mode
  • split()

Splits a string into a list.

split("-", "prod-app-frontend")  
# ["prod", "app", "frontend"]
Enter fullscreen mode Exit fullscreen mode
  • join()

Joins a list back into a string.

join("/", ["home", "user", "app"])  
# "home/user/app"
Enter fullscreen mode Exit fullscreen mode

  • Numeric Functions

Useful for calculations and dynamic sizes/counts.

  • abs()

Absolute value.

abs(-10)  # 10
Enter fullscreen mode Exit fullscreen mode
  • max() & min()
max(5, 10, 2)  # 10
min(5, 10, 2)  # 2
Enter fullscreen mode Exit fullscreen mode
  • ceil() & floor()
ceil(4.3)   # 5
floor(4.9)  # 4
Enter fullscreen mode Exit fullscreen mode
  • sum()

Adds numbers in a list.

sum([10, 20, 30])  # 60
Enter fullscreen mode Exit fullscreen mode

  • Collection Functions

Handle lists, sets, and maps effectively.

  • length()

Returns number of items.

length(["a", "b", "c"])  # 3
Enter fullscreen mode Exit fullscreen mode
  • concat()

Concatenates lists.

concat([1,2], [3,4])  
# [1,2,3,4]
Enter fullscreen mode Exit fullscreen mode
  • merge()

Merges maps.

merge(
  {a = 1},
  {b = 2}
)
# {a = 1, b = 2}
Enter fullscreen mode Exit fullscreen mode
  • reverse()

Reverses a list.

reverse([1,2,3])  
# [3,2,1]
Enter fullscreen mode Exit fullscreen mode
  • toset()

Converts list → set (removes duplicates).

toset(["dev", "dev", "prod"])  
# ["dev", "prod"]
Enter fullscreen mode Exit fullscreen mode
  • tolist()

Converts any collection to a list.

tolist(toset(["a", "b", "b"]))  
# ["a", "b"]
Enter fullscreen mode Exit fullscreen mode

Type Conversion Functions

Convert values from one type to another.

  • tonumber()
tonumber("42")  # 42
Enter fullscreen mode Exit fullscreen mode
  • tostring()
tostring(100)  # "100"
Enter fullscreen mode Exit fullscreen mode

File Functions

Used for loading configuration files, scripts, and template content.

  • file()

Reads file content as a string.

file("user-data.sh")
Enter fullscreen mode Exit fullscreen mode

fileexists()

fileexists("config.json")  # true/false
Enter fullscreen mode Exit fullscreen mode
  • dirname()

Returns directory name of a path.

dirname("/usr/local/bin/app")  
# "/usr/local/bin"
Enter fullscreen mode Exit fullscreen mode

Date add Time Functions

Useful for tagging and unique naming.

  • timestamp()

Returns current time (UTC).

timestamp()  # "2025-12-05T13:22:30Z"
Enter fullscreen mode Exit fullscreen mode
  • formatdate()
formatdate("YYYY-MM-DD", timestamp())  
# "2025-12-05"
Enter fullscreen mode Exit fullscreen mode

Validation Functions

Help validate variables and avoid errors.

  • can()

Returns true/false for safe evaluation.

can(tonumber("abc"))  # false
can(tonumber("123"))  # true
Enter fullscreen mode Exit fullscreen mode
  • regex()

Extracts patterns.

regex("[0-9]+", "app123")  
# ["123"]
Enter fullscreen mode Exit fullscreen mode
  • contains()
contains(["dev", "stage"], "dev")  
# true
Enter fullscreen mode Exit fullscreen mode
  • startswith()
startswith("prod-server", "prod")  
# true
Enter fullscreen mode Exit fullscreen mode
  • endswith()
endswith("file.txt", ".txt")  
# true
Enter fullscreen mode Exit fullscreen mode

Lookup Functions

Useful for dynamic selection of values.

  • lookup()

Safe map lookup with default value.

lookup({env="prod"}, "env", "dev")  
# "prod"
Enter fullscreen mode Exit fullscreen mode
  • element()

Gets item from list by index.

element(["a", "b", "c"], 1)  
# "b"
Enter fullscreen mode Exit fullscreen mode
  • index()

Finds index of a value in a list.

index(["dev", "stage", "prod"], "prod")  
# 2
Enter fullscreen mode Exit fullscreen mode

Conclusion
Terraform’s built-in functions allow you to:

  • Make configurations dynamic

  • Reduce repetitive code

  • Improve readability

  • Avoid runtime errors

  • Generate clean and predictable resource names

Using these functions effectively will not only simplify your Terraform code but also make your infrastructure scalable and production-ready.


Top comments (0)