Skip to main content

Terraform List

All code below also in the main.tf in the root module, unless stated otherwise.

List we use

The List we are working with

variable "LIST_1" {
type = list(string)
default = [
"ValueA",
"ValueB",
"ValueC"
]
}

Get the second element value from the List

output "OUT_LIST_1_INDEX_2" {
value = var.LIST_1[2]
}

Output

OUT_LIST_1_INDEX_2 = "ValueC"

Get all values from the List

Here we use a loop in the value.

output "OUT_LIST_1_ALL_VALUES_IN_A_FOR" {
value = [for i in var.LIST_1 : i]
}

Output

OUT_LIST_1_ALL_VALUES_IN_A_FOR = [
"ValueA",
"ValueB",
"ValueC",
]

We're getting a nice list here.

Get all values from the list, without 'for'

Well, what happens if we don't use the 'for' loop

output "OUT_LIST_1_ALL_VALUES" {
value = var.LIST_1
}

Output

OUT_LIST_1_ALL_VALUES = tolist([
"ValueA",
"ValueB",
"ValueC",
])

Per Element (in a Module)

As we did for the Map, lets create a Module in which we process each element of the List.

Calling the Module

In the root module main.tf file:

module "MODULE_LIST" {
source = "./module_list_element"
count = length(var.LIST_1)
LIST_ELEMENT = var.LIST_1[count.index]
}

Since this is a list, we call the module as many times as we have elements in the list. And that is done with a 'count' which is given a value of the length of the list.

It's like in Javascript:

for (let i = 0; i < LIST_1.length; i++) {
}

The variable which we send to the module, called LIST_ELEMENT, passes the index of that counter.

(Intermezzo) : Why not for_each?

Well, for_each does not work on Lists. If you try the following:

module "MODULE_LIST_2" {
source = "./module_list_element"
for_each = var.LIST_1
LIST_ELEMENT = each.key
}

You will get a nice error saying:

The given "for_each" argument value is unsuitable:
the "for_each" argument must be a map, or set of strings,
and you have provided a value of type list of string.

Back to our Module

The Module definition looks like this: (located in ./module_list_element/main.tf)

variable "LIST_ELEMENT" {
type = string
}

output "MODULE_LIST_ELEMENT_OUTPUT" {
value = var.LIST_ELEMENT
}

It receives a LIST_ELEMENT and sends back that same LIST_ELEMENT in an output called MODULE_LIST_ELEMENT_OUTPUT.

Show the output for the module, all occurrences

We can do various outputs with that Module

output "MODULE_LIST_ELEMENT_OUTPUT" {
value = module.MODULE_LIST
}

Output

MODULE_LIST_ELEMENT_OUTPUT = [
{
"MODULE_LIST_ELEMENT_OUTPUT" = "ValueA"
},
{
"MODULE_LIST_ELEMENT_OUTPUT" = "ValueB"
},
{
"MODULE_LIST_ELEMENT_OUTPUT" = "ValueC"
},
]

Return the value of the second object in the list

output "MODULE_LIST_ELEMENT_OUTPUT_SECOND_VALUE" {
value = module.MODULE_LIST[2].MODULE_LIST_ELEMENT_OUTPUT
}

Output

MODULE_LIST_ELEMENT_OUTPUT_SECOND_VALUE = "ValueC"

Return all values in a list

This is called a 'splat' expression in Terraform

output "MODULE_LIST_ELEMENT_OUTPUT_ALL_IN_LIST" {
value = module.MODULE_LIST[*].MODULE_LIST_ELEMENT_OUTPUT
}

The earlier versions of Terraform a deprecated form of the Splat expression was without the bracket.

module.MODULE_LIST.*.MODULE_LIST_ELEMENT_OUTPUT

It's advised not to use that anymore.

Output

MODULE_LIST_ELEMENT_OUTPUT_ALL_IN_LIST = [
"ValueA",
"ValueB",
"ValueC",
]