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.
From a list of maps, retrieve the values of the same key from each ... | This occurs with the AWS provider, but it is a basic aspect of HCL data wrangling. Using Terraform v0.12.24. I have a data source defined as follows that gives me a map of maps: # Import CFN stack exports external to Terraform (not my choice, decreed by the admins) variable "aws_private_subnets" { default = [ "PrivateSubnet1", "PrivateSubnet2", "PrivateSubnet3" ] } data "aws_cloudformation_export" "private_subnets" { for_each = toset(var.aws_private_subnets) name = each.va...
Terraform Map Variable - What It is & How to Use | What is a map variable in Terraform? See examples. The difference between map(string) and map(object).
Convert 'map' variable to list - Terraform - HashiCorp Discuss | I have been going round in circles with this problem and hope someone can help me solve it⦠I have a map variable that is passed in via TFVARs⦠(The number of records can and does change) appgw_dns_records = { "record0" = { "name" = "record1" "zone_name" = "xxxxxxxxxxxxxxx.co.uk" }, "record1" = { "name" = "record2" "zone_name" = "xxxxxxxxxxxxxxx.co.uk" } } I want to use information from the map later on in my code as a list. The format I need is this⦠[ārecord1.x...
Terraform handling list of maps ā faultbucket | Jul 22, 2020 ... Dropping a quick reference here on some specific use cases for Terraform syntax. Scenario #1. I want to build an Azure Route Table.
Type Constraints - Configuration Language | Terraform | HashiCorp ... | The distinctions are only useful when restricting input values for a module or resource. Conversion of Complex Types. Similar kinds of complex types (list/tupleĀ ...