slide

Terraform fot d element()

Ned Bellavance
2 min read

Cover

This is part of an ongoing series of posts documenting the built-in interpolation functions in Terraform. For more information, check out the beginning post. In this post I am going to cover the element() function. The example file is on GitHub here.

What is it?

Function name: element(list, index)

Returns: Takes a list and an index value. Returns the element from the list with the submitted index value.

Example:

variable "element" {
  default = ["one","two","three"]
}

# Returns "one"
output "element_output" {
  value = "${element(var.element,0)}"
}

Example file:

##############################################
# Function: element
##############################################
##############################################
# Variables
##############################################
variable "string_list" {
  default = ["So", "long", "and", "thanks"]
}

variable "int_list" {
  default = [41, 42, 43]
}

variable "empty_string_list" {
  default = ["", "", ""]
}

variable "empty_list" {
  default = []
}

variable "bool_list" {
  default = [false, true]
}

##############################################
# Resources
##############################################

##############################################
# Outputs
##############################################

#Test a standard list
output "1_element_list" {
  value = "${element(var.string_list,1)}"
}

#Test length
output "1b_element_list" {
  value = "${element(var.string_list,length(var.string_list)-1)}"
}

#Test wrap
output "1c_element_list" {
  value = "${element(var.string_list,10)}"
}

#Test a standard int list
output "2_int_list" {
  value = "${element(var.int_list,2)}"
}

#Test list of empty strings
output "3_empty_string_list" {
  value = "${element(var.empty_string_list,2)}"
}

#Test what will happen with a list boolean values
output "4_bool_list" {
  value = "${element(var.bool_list,2)}"
}

Run the following from the element folder to get example output for a number of different cases:

#All examples are in variables
terraform apply

Why use it?

There are a ton of cases I can think of where I want an element from a list. Many of them involve looping through the list. Let’s say you grab all the availability zones for an AWS region. You can alternate the placement of resources by using element in a count loop. The function will do a standard mod function on the index, so if you have 3 elements and the index value is 4, then you will get the 4 mod 3 element or the element at index 1. Remember that index values start at 0.

Lessons Learned

The function explicitly doesn’t deal with nested lists, so you’ll need to use flatten first. Boolean values are converted to 0 and 1. While the function is happy to take a positive number greater than the number of elements, it will not accept a negative number. It will also not process an empty list. You may want to clean up your list ahead of time using functions like sort and compact.

Coming up next is the file() function.