Skip to main content
In this lesson, we explore several built-in functions available in Terraform. Throughout your Terraform journey, you have seen functions like file (to read file contents), length (to count elements in lists or maps), and toset (to convert lists into sets). Until now, you may have used these functions within configuration files; however, we haven’t examined in detail how they transform and combine values. Thankfully, Terraform’s interactive console allows you to experiment with these functions and interpolations before integrating them into your configurations. To start the interactive console, run:
The console loads the state associated with your current configuration directory along with any defined variables. This setup lets you test and fine-tune functions and interpolations to ensure they work as expected. Below is a sample configuration that demonstrates the usage of several functions:
In the console, you can test these functions. For example, using the file function displays the contents of a specified file (such as the main.tf file). Running the length function on the region variable returns 3, while the toset function converts the list and removes duplicate values:
In this example, the duplicate "us-east-1" is removed when the list is converted to a set.
Terraform offers many other useful built-in functions. In the following sections, we delve into numeric, string, collection, and map functions to enhance your configuration authoring ability.

Numeric Functions

Numeric functions help you perform operations on numbers. For instance, the max function returns the largest value among its arguments, while the min function returns the smallest. When using variables as arguments, you can expand a set of values using the expansion operator (...). Consider this variable declaration:
You can test numeric functions in the console:
Other useful numeric functions include:
  • ceil: Rounds a floating-point number to the smallest integer greater than or equal to the number (e.g., both 10.1 and 10.9 round up to 11).
  • floor: Rounds a number down to the largest integer that is less than or equal to the provided value.

String Functions

String functions allow you to manipulate text data effectively. One valuable function is split, which converts a string into a list based on a specified separator. For example, if you have a string containing dummy AMI IDs separated by commas, you can split the string into a list:
Test the split function in the Terraform console:
Additional string manipulation functions include:
  • lower: Converts all characters to lowercase.
  • upper: Converts all characters to uppercase.
  • title: Capitalizes the first letter of each word.
  • substring: Extracts a portion of a string using an offset (zero-based indexing) and a specified length.
  • join: Concatenates a list of strings into a single string.

Collection Functions

Collection functions are designed to work with data types such as sets, lists, and maps. Previously, you used the length function to count list elements; other useful functions include:
  • index: Returns the index of the first occurrence of a specified element in a list. For example, to find the position of "AMI-ABC" in the ami variable:
  • element: Retrieves the element at a specific index in a list. For instance, to get the element at index 2:
  • contains: Checks if a list contains a specified element, returning true or false:
Consider the following variable declaration for a list of AMI IDs:
Testing these functions in the console demonstrates how you can interactively examine collection data.

Map Functions

Map functions operate on map data types. In this section, we update the ami variable to be a map that associates AWS regions with corresponding AMI IDs:
Terraform offers several useful map functions:
  • keys: Returns a list of keys from a map. For instance, the following example demonstrates how to extract all keys from the ami map:
The image displays a menu with four buttons labeled: Numeric Functions, String Functions, Collection Functions, and Type Conversion Functions.
In the console:
  • values: Returns a list of values from the map.
  • lookup: Retrieves a value for a specified key from a map. It accepts the map and key as arguments and throws an error if the key is not found. To avoid errors, you can supply a default value as a third parameter:
If the key is missing, such as "us-west-2", the following error appears:
By providing a default value, Terraform returns that value instead:
In this case, when "us-west-2" is absent from the map, Terraform returns the supplied default "ami-pqr".
This lesson has covered a variety of built-in functions ranging from numeric and string operations to collection and map manipulations. Mastering these functions is essential for creating dynamic, robust Terraform configurations that are both efficient and scalable. For additional guidance, explore the Terraform Documentation and related resources.

Watch Video