Skip to main content
In this lesson, we’ll explore additional functions available in Terraform. Throughout this course, we’ve used functions like file to read file data, length to count elements in lists or maps, and toSet to convert lists into sets. However, we haven’t yet taken a closer look at how these functions process or transform values. Fortunately, Terraform’s interactive console allows you to test functions and interpolations before using them in your configurations.
For a safe and interactive testing environment, always validate changes in the console to prevent unexpected results in production.

Launching the Interactive Console

To start the interactive console, run:
By default, the console loads the state from your current configuration directory and includes any variables defined in your configuration files, making it an ideal environment for experimentation. Below is an example configuration that demonstrates the use of the file, length, and toSet functions:
After launching the console, you can run commands like these:
The output confirms that the region list variable contains three elements. Note that using toSet with duplicate values (like “us-east-1”) automatically removes duplications because sets do not allow duplicates. Terraform offers dozens of built-in functions to help you manage your configuration data. In the following sections, we’ll review some commonly used functions categorized by their use cases: numeric operations, string manipulation, collection processing, and map handling.

Numeric Functions

Numeric functions are useful for manipulating numerical values. For example, the max function returns the largest number among its arguments, while min returns the smallest. When working with sets or lists of numbers stored in a variable, you can use the expansion operator (...) to pass individual arguments. Consider the following variable definition:
You can experiment with numeric functions in the Terraform console:
Other commonly used numeric functions include:
  • ceil: Rounds a number up to the nearest whole integer.
  • floor: Rounds a number down to the nearest whole integer.
Example usage:

String Functions

String functions help with transforming and manipulating text data. One useful function is split, which turns a string into a list based on a specified separator. For example, if you have a comma-separated string of AMI IDs, you can convert it into a list. Here is a variable definition containing AMI IDs:
Test the split operation in the console:
To change the case of a string, use:
  • lower: Converts the string to lowercase.
  • upper: Converts the string to uppercase.
  • title: Converts the first character of each word to uppercase.
Example usage:
The substr function extracts a substring by specifying an offset and a length. Note that indexes start at zero. For instance:

Collection Functions

Collection functions work with data types such as sets, lists, and maps. The length function, for example, returns the number of elements in a list. Consider this variable:
Determine its length using:
Other useful collection functions include:
  • index: Finds the index of a specific element in a list.
  • element: Retrieves the element at a particular index.
  • contains: Checks if a list contains a specific element, returning true or false.
Example usage:
Remember that list indexing starts at zero, and the contains function is case-sensitive.

Map Functions

Map functions allow you to work with key-value pairs. Consider a variable where AMI IDs are stored in a map with regions as keys:
You can convert the map into lists of keys or values using the keys and values functions:
To retrieve a value for a specific key, use the lookup function. It takes the map and the key as arguments. If the key is missing, the function raises an error unless a default value is provided. Example without a default value:
Providing a default value ensures safe lookups:
In this example, since the key “us-west-2” does not exist in the map, the lookup function returns “ami-PQR” as the default value.

Conclusion

This lesson provided an overview of various Terraform functions used for numeric operations, string manipulation, collection handling, and map lookups. By experimenting with these functions in the Terraform console, you can dynamically manipulate and transform data within your configurations, ultimately making your Terraform scripts more efficient and flexible. For more details on Terraform functions and best practices, visit the Terraform Documentation.

Watch Video