DEV Community

Cover image for Terraform Functions Part-1
Adarsh Gupta
Adarsh Gupta

Posted on

Terraform Functions Part-1

 Terraform is more than just an infrastructure provisioning tool—it is a powerful configuration language that helps you automate cloud deployments efficiently. One of the most valuable components of Terraform is its rich set of built-in functions, which allow you to manipulate data, transform inputs, and create clean, dynamic configurations.

While Terraform does not support custom functions, its inbuilt functions are more than enough to handle real-world infrastructure requirements.

You can open the console using:

terraform console
Enter fullscreen mode Exit fullscreen mode

1. String Functions

String functions are extremely useful for naming resources, creating tags, formatting paths, or transforming any text values.

Examples

upper("string")
# Output: "STRING"
Enter fullscreen mode Exit fullscreen mode
lower("STRING")
# Output: "string"
Enter fullscreen mode Exit fullscreen mode
trim("  adarsh  ", " ")
# Output: "adarsh"
Enter fullscreen mode Exit fullscreen mode
replace("hello adarsh", " ", "-")
# Output: "hello-adarsh"
Enter fullscreen mode Exit fullscreen mode
substr("adarsh", 0, 3)
# Output: "ada"
Enter fullscreen mode Exit fullscreen mode

2. Numeric Functions

Numeric functions are useful when calculating dynamic sizes, thresholds, limits, cost-based decisions, or autoscaling configurations.

Examples

max(34, 2, 24)
# Output: 34
Enter fullscreen mode Exit fullscreen mode
min(34, 2, 24)
# Output: 2
Enter fullscreen mode Exit fullscreen mode
abs(-45)
# Output: 45
Enter fullscreen mode Exit fullscreen mode

3. Collection Functions

Collections (lists, sets, maps) are foundational in Terraform. These functions allow you to transform, combine, and organize them efficiently.

length

length([1,3,4])
# Output: 3
Enter fullscreen mode Exit fullscreen mode

concat

concat([1,2], [56,45])
# Output: [1,2,56,45]
Enter fullscreen mode Exit fullscreen mode

merge

merge({a=1}, {b=24})
# Output: { "a" = 1, "b" = 24 }
Enter fullscreen mode Exit fullscreen mode

split — convert string to list

split(",", "a,b,c")
# Output: ["a", "b", "c"]
Enter fullscreen mode Exit fullscreen mode

join — convert list back to string

join("-", ["hello", "adarsh"])
# Output: "hello-adarsh"
Enter fullscreen mode Exit fullscreen mode

split + join is especially useful when parsing CSV values, dynamic tags, or environment names.


4. Lookup Function

The lookup function provides a safe way to fetch values from a map. It prevents errors in case the key doesn’t exist and allows you to specify a default.

Example

lookup({ env = "prod", region = "ap-south-1" }, "region", "default-region")
# Output: "ap-south-1"
Enter fullscreen mode Exit fullscreen mode

If the key is not found, Terraform returns the fallback value you provide.


5. Type Conversion Functions

Terraform often requires converting values between data types—especially when reading variables, external inputs, or combining different modules.

Examples

toset(["a","b","a"])
# Output: toset(["a","b"])
Enter fullscreen mode Exit fullscreen mode
tonumber("23")
# Output: 23
Enter fullscreen mode Exit fullscreen mode
tostring(34)
# Output: "34"
Enter fullscreen mode Exit fullscreen mode

6. Date & Time Functions

These functions help you generate timestamps and format date strings, often used in tagging, versioning, and resource naming.

Examples

timestamp()
# Output: "2025-12-06T13:25:23Z"
Enter fullscreen mode Exit fullscreen mode
formatdate("DD-MM-YY", timestamp())
# Output: "06-12-25"
Enter fullscreen mode Exit fullscreen mode

Useful CLI Commands When Working With Functions

A few Terraform commands help you experiment and keep your workspace up to date:

  • terraform refresh
    Updates the state file to reflect actual resource details.

  • terraform init -upgrade
    Downloads the latest compatible provider versions.

Both commands are helpful when testing changes or adding new functions to your configuration.


Conclusion

Terraform functions are essential tools that help you write clean, efficient, and highly dynamic infrastructure code. Whether it’s manipulating strings, performing calculations, splitting lists, joining text, or safely looking up values, these functions make your Terraform configurations far more flexible and reusable.

Mastering these foundational functions will significantly improve your ability to work with complex Terraform projects and create production-ready infrastructure automation.


Reference Video — Terraform Functions Explained


@piyushsachdeva

Top comments (0)