Learn how to effectively map a list of maps in Terraform to extract specific field values and create a new list tailored to your needs.
In Terraform, working with lists of maps often involves extracting values associated with a specific key. This can be achieved efficiently using a combination of the for
expression and the ...
operator. Let's explore how to extract a list of values for a given key from a list of maps.
To extract a list of values for a specific key from a list of maps in Terraform, you can use the for
expression with the ...
operator.
variable "list_of_maps" {
default = [
{"name" = "foo", "value" = "bar"},
{"name" = "baz", "value" = "qux"}
]
}
output "values" {
value = [for map in var.list_of_maps : map.value]
}
This code will iterate over each map in the list_of_maps
variable and extract the value associated with the "value" key. The resulting values
output will be a list containing ["bar", "qux"].
Here's a breakdown of the code:
for map in var.list_of_maps
: This iterates over each map in the list_of_maps
variable, assigning each map to the map
variable.map.value
: This accesses the value associated with the "value" key in the current map
.[ ... ]
: This constructs a new list containing the extracted values.You can use this approach to extract values for any key from a list of maps. Simply replace "value"
with the desired key name.
This Terraform code defines a variable containing a list of maps, each with "name" and "value" keys. It then extracts the "value" and "name" values from each map into separate lists, outputting them as "values" and "names" respectively.
# Define a variable with a list of maps
variable "list_of_maps" {
default = [
{"name" = "foo", "value" = "bar"},
{"name" = "baz", "value" = "qux"},
{"name" = "quux", "value" = "corge"}
]
}
# Extract the "value" from each map and create a new list
output "values" {
value = [for map in var.list_of_maps : map.value]
}
# Extract the "name" from each map and create a new list
output "names" {
value = [for map in var.list_of_maps : map.name]
}
Explanation:
variable "list_of_maps"
: This defines a variable named list_of_maps
that holds a list of maps. Each map has two keys: "name" and "value".
output "values"
: This defines an output variable named values
.
value = [for map in var.list_of_maps : map.value]
: This is the core logic for extracting values. Let's break it down:
for map in var.list_of_maps
: This iterates over each map within the list_of_maps
variable. In each iteration, the current map is assigned to the temporary variable map
.map.value
: This accesses the value associated with the key "value" within the current map
.[...]
: The square brackets enclose the entire expression, indicating that the result should be a new list.output "names"
: This output variable is similar to output "values"
, but it extracts the values associated with the "name" key instead of "value".
Output:
When you run Terraform with this code, the outputs will be:
values = [
"bar",
"qux",
"corge"
]
names = [
"foo",
"baz",
"quux"
]
This demonstrates how to effectively extract specific key values from a list of maps in Terraform using the for
expression and the ...
operator.
map.nonexistent_key
) will result in an error. To handle this gracefully, you can use the can
function within the for
expression:
output "optional_values" {
value = [for map in var.list_of_maps : map.optional_key if can(map.optional_key)]
}
optional_key
.for
expression with ...
is generally the most readable and efficient way to extract values, you can also achieve similar results using functions like lookup
within a loop. However, this can be less performant for larger lists.variable "key_to_extract" {
default = "value"
}
output "dynamic_values" {
value = [for map in var.list_of_maps : map[var.key_to_extract]]
}
This table summarizes how to extract a list of values for a specific key from a list of maps in Terraform:
Feature | Description | Example |
---|---|---|
Data Structure | A list of maps, where each map represents a set of key-value pairs. | [{"name" = "foo", "value" = "bar"}, {"name" = "baz", "value" = "qux"}] |
Goal | Extract the values associated with a specific key from all maps in the list. | Get all values associated with the key "value" . |
Method | Use a for expression with the spread operator (... ) to iterate over the list and access the desired key in each map. |
[for map in var.list_of_maps : map.value] |
Explanation | - for map in var.list_of_maps : Iterates through each map in the list. - map.value : Accesses the value associated with the "value" key in the current map. - [ ... ] : Constructs a new list containing the extracted values. |
|
Result | A new list containing only the extracted values. | ["bar", "qux"] |
Adaptability | Replace "value" with any desired key to extract values for that key. |
Extracting specific values from lists of maps is a common task in Terraform, especially when dealing with complex data structures returned by providers or modules. By leveraging the for
expression and the ...
operator, you can efficiently create new lists containing only the desired values. Understanding this pattern empowers you to work with structured data effectively, enabling more dynamic and reusable Terraform configurations. Remember to handle optional keys gracefully and consider using type constraints for improved code clarity and maintainability. As you become more comfortable with these techniques, you'll be able to handle increasingly complex data manipulation scenarios in your infrastructure as code.