DEV Community

Cover image for Mastering Terraform Functions: A Guide with Examples ๐Ÿ‘จโ€๐Ÿ’ป
Arun Singh Sisodiya
Arun Singh Sisodiya

Posted on • Originally published at Medium

Mastering Terraform Functions: A Guide with Examples ๐Ÿ‘จโ€๐Ÿ’ป

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

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:

<function_name>(arg 1, arg 2)
Enter fullscreen mode Exit fullscreen mode

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(): This function is used to format a string. It takes one or more arguments and returns a formatted string.

    Syntaxโ†’ `fomrat(spec, values...)`
    
    E.g.
    > format("Hello, %s!", "Terraform")
    Hello Terraform
    > format("There are %d lights", 4)
    There are 4 lights
    
  • 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)

    E.g.
    > join(", ", ["foo", "bar", "baz"])
    foo, bar, baz
    > join(", ", ["foo"])
    foo
    
  • lookup(): This function is used to look up a value in a map. It takes two arguments: the map and the key.

    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(): abs returns the absolute value of the given number.

    > abs(23)
    23
    > abs(0)
    0
    > abs(-12.4)
    12.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.

    Syntaxโ†’ log(number, base)

    E.g.
    > log(50, 10)
    1.6989700043360185
    > log(16, 2)
    4
    
  • 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:

    > max(12, 54, 3)
    54
    > max([12, 54, 3]...)
    54
    
  • 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:

    > min(12, 54, 3)
    3
    > 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
    
  • signum(): signum determines the sign of a number, returning a number between -1 and 1 to represent the sign.

    > signum(-13)
    -1
    > signum(0)
    0
    > signum(344)
    1
    
  • 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.

    > 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(): lower converts all cased letters in the given string to lowercase.

    > lower("HELLO")
    hello
    > lower("ะะ›ะ›ะž!")
    ะฐะปะปะพ!
    
  • upper(): upper converts all cased letters in the given string to uppercase.

    > upper("hello")
    HELLO
    > upper("ะฐะปะปะพ!")
    ะะ›ะ›ะž!
    
  • replace(): replace searches a given string for another given substring, and replaces each occurrence with a given replacement string.

    Syntaxโ†’ `replace(string, substring, replacement)`
    
    E.g.
    > replace("1 + 2 + 3", "+", "-")
    1 - 2 - 3
    
    > replace("hello world", "/w.*d/", "everybody")
    hello everybody
    
  • trim(): trim removes the specified set of characters from the start and end of the given string.

    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

๐ŸŒŸ 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! ๐Ÿ”ฅ

Top comments (0)