Terraform – FotD – map()

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 map() function. The example file is on GitHub here.

What is it?

Function name: map(key, value,…)

Returns: Takes a set of keys and values, alternating, and returns a map based off the key/value pairs. All values must be of the same type. No duplicate keys are allowed.

Example:

variable "value" {
  default = "value"
}

# Returns { key = value }
output "map_output" {
  value = "${map("key",var.value)}"
}

Example file:

##############################################
# Function: map
##############################################
##############################################
# Variables
##############################################

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

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

output "1_basic_map" {
  value = "${map("key1","value1","key2","value2")}"
}

output "2_map_lists" {
  value = "${map("life",list("42"),"the universe",list("six","times","seven"))}"
}

output "3_empty_map" {
  value = "${map()}"
}

output "4_nested_map" {
  value = "${
    map(
      "nested1", map("n1k1","n1v1","n1k2","n1v2"),
      "nested2", map("n2k1","n2v1","n2k2","n2v2")
    )}"
}

output "5_triple_nested" {
  value = "${
    map(
      "level1", map(
        "level2", map(
          "level3","infinity"
        )
      )
    )
  }"
}

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

#All examples are in output
terraform apply

Why use it?

Sometimes you just need a map. I’ve been using this function to generate maps for other functions when creating these posts. But this is also a convenient way to package up information from something that maybe doesn’t output the map data type you wanted. If you’re reading in data using an external data source, then you could use map to construct a map from that external information and then parse that out for something like a firewall rule set.

Lessons Learned

The function does precisely what you would expect. I was pleasantly surprised to see that it let me create nested maps and triple nested maps. I don’t know how far down you could go with the nesting, but it works to three levels. Beyond that you are probably just probing the limit for the fun of it, rather than having some practical application. One of the limitations here is that the values of a map need to be the same data type, so I can’t mix strings, lists, and maps for the value. Not sure why I would want to, but also not sure why the limitation exists.

Coming up next is the matchkeys() function, which appears to be hella confusing in the docs.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.