Mastering Terraform Functions: A Guide with Examples 👨‍💻
Tags: terraform, tutorial, devops, cloud
Published: 2023-02-25 10:52:20

Discover the power of Terraform functions and learn how to use them to simplify your infrastructure as code. From filesystem functions to collection and encoding functions, we cover it all with examples and images.

![Terraform banner](https://cdn-images-1.medium.com/max/4800/1*y0HZCbscmEJoWtG9LMaueA.png)

## Introduction

In this blog post, we’ll explore the world of Terraform functions and how they can help you write more concise and powerful infrastructure as code. With Terraform functions, you can perform advanced data manipulation, simplify variable interpolation, and work with files and directories. We’ll show you how to use Terraform functions in your code, with examples and images to guide you.

## What are Terraform functions?

👋 Hey there, fellow Terraform users! Are you looking to level up your infrastructure-as-code game? Well, have you considered using Terraform functions? 🤔

Terraform functions allow you to perform various operations on your Terraform code. They can generate dynamic values, manipulate strings, and perform mathematical calculations, among other things. This blog post will cover some of the most common Terraform functions and how you can use them in your code.

Terraform functions are used in the expression of an argument and return a value of a specified type. The built-in functions can be generalized using the syntax below:

```bash
<function_name>(arg 1, arg 2)
```

The number and type of arguments accepted by Terraform functions are predefined. The Terraform language includes several built-in functions that you can call from within expressions to transform and combine values.

## 🌟 Interpolation Functions 🌟

Interpolation functions are used to insert a value into a string. They can be used in resource configuration blocks, data source configuration blocks, and other parts of your Terraform code.

Let’s see some examples and do some hands-on:

* [format()](https://developer.hashicorp.com/terraform/language/functions/format): This function is used to format a string. It takes one or more arguments and returns a formatted string.

     ```bash
    Syntax→ `fomrat(spec, values...)`

    E.g.
    > format("Hello, %s!", "Terraform")
    Hello Terraform
    > format("There are %d lights", 4)
    There are 4 lights
    ```

* [join()](https://developer.hashicorp.com/terraform/language/functions/join): This function is used to concatenate a list of strings. It takes two arguments: a separator and a list of strings.

    Syntax→ `join(separator, list)`

    ```bash
    E.g.
    > join(", ", ["foo", "bar", "baz"])
    foo, bar, baz
    > join(", ", ["foo"])
    foo
    ```

* [lookup()](https://developer.hashicorp.com/terraform/language/functions/lookup): This function is used to look up a value in a map. It takes two arguments: the map and the key.

    ```bash
    Syntax→ `lookup(map, key, default)`

    E.g.
    > lookup({a="ay", b="bee"}, "a", "what?")
    ay
    > lookup({a="ay", b="bee"}, "c", "what?")
    what?
    ```

## 🌟 Numeric Functions 🌟

Numeric functions are used to perform mathematical calculations. They can be used in resource configuration blocks, data source configuration blocks, and other parts of your Terraform code.

Let’s see some examples and do some hands-on:

* [abs()](https://developer.hashicorp.com/terraform/language/functions/abs): abs returns the absolute value of the given number.

    ```bash
    > abs(23)
    23
    > abs(0)
    0
    > abs(-12.4)
    12.4
    ```

* [ceil()](https://developer.hashicorp.com/terraform/language/functions/ceil): ceil returns the closest whole number that is greater than or equal to the given value, which may be a fraction.

    ```bash
    > ceil(5)
    5
    > ceil(5.1)
    6
    ```

* [floor()](https://developer.hashicorp.com/terraform/language/functions/floor): floor returns the closest whole number that is less than or equal to the given value, which may be a fraction.

    ```bash
    > floor(5)
    5
    > floor(4.9)
    4
    ```

* [log()](https://developer.hashicorp.com/terraform/language/functions/log): log returns the logarithm of a given number in a given base.

    Syntax→ log(number, base)

    ```bash
    E.g.
    > log(50, 10)
    1.6989700043360185
    > log(16, 2)
    4
    ```

* [max()](https://developer.hashicorp.com/terraform/language/functions/max): max takes one or more numbers and returns the greatest number from the set. If the numbers are in a list or set value, use ... to expand the collection to individual arguments:

    ```bash
    > max(12, 54, 3)
    54
    > max([12, 54, 3]...)
    54
    ```

* [min()](https://developer.hashicorp.com/terraform/language/functions/min): min takes one or more numbers and returns the smallest number from the set.If the numbers are in a list or set value, use ... to expand the collection to individual arguments:

    ```bash
    > min(12, 54, 3)
    3
    > min([12, 54, 3]...)
    3
    ```

* [pow()](https://developer.hashicorp.com/terraform/language/functions/pow): pow calculates an exponent, by raising its first argument to the power of the second argument.

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

* [signum()](https://developer.hashicorp.com/terraform/language/functions/signum): signum determines the sign of a number, returning a number between -1 and 1 to represent the sign.

    ```bash
    > signum(-13)
    -1
    > signum(0)
    0
    > signum(344)
    1
    ```

* [parseint()](https://developer.hashicorp.com/terraform/language/functions/parseint): parseint parses the given string as a representation of an integer in the specified base and returns the resulting number. The base must be between 2 and 62 inclusive. All bases use the arabic numerals 0 through 9 first. Bases between 11 and 36 inclusive use case-insensitive latin letters to represent higher unit values. Bases 37 and higher use lowercase latin letters and then uppercase latin letters. If the given string contains any non-digit characters or digit characters that are too large for the given base then parseint will produce an error.

    ```bash
    > parseint("100", 10)
    100
    
    > parseint("FF", 16)
    255
    
    > parseint("-10", 16)
    -16
    
    > parseint("1011111011101111", 2)
    48879
    
    > parseint("aA", 62)
    656
    
    > parseint("12", 2)
    
    Error: Invalid function argument
    
    Invalid value for "number" parameter: cannot parse "12" as a base 2 integer.
    ```

## 🌟 String Functions 🌟

String functions are used to manipulate strings. They can be used in resource configuration blocks, data source configuration blocks, and other parts of your Terraform code.

Let’s see some examples and do some hands-on:

* [lower()](https://developer.hashicorp.com/terraform/language/functions/lower): lower converts all cased letters in the given string to lowercase.

    ```bash
    > lower("HELLO")
    hello
    > lower("АЛЛО!")
    алло!
    ```

* [upper()](https://developer.hashicorp.com/terraform/language/functions/upper): upper converts all cased letters in the given string to uppercase.

    ```bash
    > upper("hello")
    HELLO
    > upper("алло!")
    АЛЛО!
    ```

* [replace()](https://developer.hashicorp.com/terraform/language/functions/replace): replace searches a given string for another given substring, and replaces each occurrence with a given replacement string.

    ```bash
    Syntax→ `replace(string, substring, replacement)`

    E.g.
    > replace("1 + 2 + 3", "+", "-")
    1 - 2 - 3
    
    > replace("hello world", "/w.*d/", "everybody")
    hello everybody
    ```

* [trim()](https://developer.hashicorp.com/terraform/language/functions/trim): trim removes the specified set of characters from the start and end of the given string.

    ```bash
    Syntax→ `trim(string, str_character_set)`

    E.g.
    > trim("?!hello?!", "!?")
    "hello"
    
    > trim("foobar", "far")
    "oob"
    
    > trim("   hello! world.!  ", "! ")
    "hello! world."
    ```

> There are many more terraform functions. You can find the details here: [https://developer.hashicorp.com/terraform/language/functions](https://developer.hashicorp.com/terraform/language/functions)

## 🌟 Conclusion 🌟

Terraform functions can help you write more efficient and dynamic infrastructure code. By using these functions, you can easily manipulate strings, perform mathematical calculations, and generate dynamic values. So give them a try and see how they can improve your Terraform workflows! 🔥