This lesson explores additional Terraform functions for processing and transforming values, including numeric, string, collection, and map functions.
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.
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:
Copy
Ask AI
resource "aws_iam_policy" "adminUser" { name = "AdminUsers" policy = file("admin-policy.json")}resource "local_file" "pet" { filename = var.filename count = length(var.filename)}resource "local_file" "pet" { filename = var.filename for_each = toset(var.region)}variable "region" { type = list(string) default = ["us-east-1", "us-east-1", "ca-central-1"] description = "A list of AWS regions"}
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 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:
Copy
Ask AI
variable "num" { type = set(number) default = [250, 10, 11, 5] description = "A set of numbers"}
You can experiment with numeric functions in the Terraform console:
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:
Copy
Ask AI
variable "ami" { type = string default = "ami-xyz,AMI-ABC,ami-efg" description = "A string containing AMI IDs"}
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:
Copy
Ask AI
variable "ami" { type = list(string) default = ["ami-xyz", "AMI-ABC", "ami-efg"] description = "A list of AMI IDs"}
Determine its length using:
Copy
Ask AI
$ terraform console> length(var.ami)3
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.
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:
Copy
Ask AI
variable "ami" { type = map(string) default = { "us-east-1" = "ami-xyz", "ca-central-1" = "ami-efg", "ap-south-1" = "ami-ABC" } description = "A map of AMI IDs for specific regions"}
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:
Copy
Ask AI
$ terraform console> lookup(var.ami, "ca-central-1")ami-efg> lookup(var.ami, "us-west-2")Error: Error in function call on <console-input> line 1: (source code not available)Call to function "lookup" failed: lookup failed to find 'us-west-2'.
Providing a default value ensures safe lookups:
Copy
Ask AI
variable "ami" { type = map(string) default = { "us-east-1" = "ami-xyz", "ca-central-1" = "ami-efg", "ap-south-1" = "ami-ABC" } description = "A map of AMI IDs for specific regions"}
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.