slide

Terraform fot d index()

Ned Bellavance
3 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 index() function. The example file is on GitHub here.

What is it?

Function name: index(list, value)

Returns: Takes a list and looks for a value in the list matching the submitted value. Returns the index number of the first item found in the list with a matching value.

Example:

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

# Returns 1
output "index_output" {
  value = "${index(var.list,"two")}"
}

Example file:

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

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

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

variable "empty_list" {
  default = []
}

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

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

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

#Test a standard list with more than one matching element
output "1a_index_list" {
  value = "${index(var.string_list,"So")}"
}

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

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

#Test what will happen with boolean values
output "4_bool_list" {
  value = "${index(var.bool_list,1)}"
}

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

#Default examples
terraform apply

#Cannot find list element
terraform apply -var "string_list=[]"

#int or string doesn't matter
terraform apply -var 'int_list=["41","42"]'

Why use it?

This function is sort of the inverse of the element function. Rather than supplying an index and getting the element, we are supplying the element and getting the index. I can only assume you would want to then slice up the list in some way based off where an element is in the list. To that end, you would probably want to do a little list hygiene beforehand. I imagine you would use things like sort, chomp, and distinct to do that.

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, so you will need to search the list for 0 or 1. The function doesn’t really differentiate between ints and strings, so it works just as well to search for a string in a list of ints. The function only returns the first index of the value, even if the value appears multiple times. You could probably use that to your advantage if you have combined multiple lists that have a common ending element. Now you can get the index of where the original first list ended, and slice it off.

Coming up next is the join() function.