askvity

What Are Maps in Terraform?

Published in Terraform Data Types 3 mins read

In Terraform, a “map” is a data structure used to represent a collection of key-value pairs. As the reference states, it is similar to an object or dictionary in other programming languages. Maps are a fundamental data type in Terraform's HashiCorp Configuration Language (HCL), which is used to define infrastructure as code (IaC) in Terraform.

Maps provide a structured way to group related settings or configurations, allowing you to access specific values using unique string keys.

Understanding Key-Value Pairs

A key-value pair consists of:

  • Key: A unique identifier (always a string) within the map.
  • Value: The data associated with the key. This can be any valid Terraform type, such as a string, number, boolean, list, or even another map or object.

Think of it like a physical dictionary where the "key" is the word you look up, and the "value" is its definition.

Why Use Maps in Terraform?

Maps are incredibly useful for:

  • Organizing Variables: Grouping related configuration settings for resources or modules.
  • Dynamic Configuration: Using map values to configure resources based on keys (e.g., instance sizes per environment).
  • Lookup Tables: Mapping input values to output values or configurations.
  • Handling Resource Attributes: Many resource attributes accept or return maps.

Defining and Using Maps

You can define maps explicitly using curly braces {} with key = value syntax.

Declaring a Map Variable

variable "instance_types" {
  description = "Map of instance types per environment."
  type        = map(string) # Specifies keys map to string values
  default = {
    "development" = "t3.micro"
    "staging"     = "t3.small"
    "production"  = "m5.large"
  }
}

In this example, "development", "staging", and "production" are the keys, and "t3.micro", "t3.small", and "m5.large" are their corresponding string values.

Accessing Map Elements

You can access a specific value from a map using square bracket [] notation with the key name.

resource "aws_instance" "app_server" {
  # ... other configuration ...
  instance_type = var.instance_types["production"] # Accessing the value for 'production'
  # This would resolve to "m5.large"
}

Attempting to access a key that doesn't exist in the map will result in an error.

Map Types

Terraform supports explicit type constraints for maps, which improves code clarity and helps catch errors early. Common map types include:

  • map(string): Keys map to string values.
  • map(number): Keys map to number values.
  • map(bool): Keys map to boolean values.
  • map(list(...)): Keys map to list values of a specific type.
  • map(object(...)): Keys map to complex object values.
  • map(any): Keys can map to values of any type (less strict).
variable "complex_config" {
  type = map(object({
    port     = number
    enabled  = bool
    features = list(string)
  }))
  default = {
    "service_a" = {
      port     = 8080
      enabled  = true
      features = ["logging", "metrics"]
    }
    "service_b" = {
      port     = 9000
      enabled  = false
      features = ["caching"]
    }
  }
}

Maps are essential building blocks in Terraform for creating flexible, readable, and maintainable infrastructure configurations.

Related Articles