HCL Language – 17 – List and Map Interpolation

List and Map Interpolation in HCL

HCL (HashiCorp Configuration Language) provides powerful capabilities for working with complex data structures. List and map interpolation is a crucial feature that allows you to work with lists and maps within your Terraform configurations, making it easier to define dynamic and flexible infrastructure. In this guide, we’ll explore the concepts and usage of list and map interpolation in HCL.

List Interpolation

List interpolation in HCL involves embedding lists within your configurations. You can reference, merge, and manipulate lists using interpolation.


variable "tags" {
  type = list(string)
  default = ["app", "web", "prod"]
}

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
  tags = {
    Name  = "web-instance"
    Group = join(", ", var.tags)
  }
}
    

In this example, the “tags” variable is a list of strings that represents labels for the instance. The “join” function is used to concatenate the elements of the “var.tags” list into a single string, creating a dynamic “Group” tag like “app, web, prod.”

Map Interpolation

Map interpolation allows you to embed maps and merge them into your configurations. This is especially useful for creating complex resource configurations.


variable "config" {
  type = map(string)
  default = {
    key1 = "value1"
    key2 = "value2"
  }
}

resource "example_resource" "example" {
  name        = "my-resource"
  additional_config = var.config
}
    

In this case, the “config” variable is a map with key-value pairs representing additional configuration settings. The “additional_config” attribute of the resource directly references the “var.config” map, allowing you to pass complex configuration data easily.

Advanced List and Map Interpolation

HCL provides advanced features for working with lists and maps, including merging, filtering, and transforming data. These capabilities enable you to manage and modify complex data structures.


variable "app_servers" {
  type = list(object({
    name = string
    port = number
  }))
  default = [
    { name = "server1", port = 8080 },
    { name = "server2", port = 8081 }
  ]
}

resource "aws_lb" "example" {
  name = "my-loadbalancer"
  internal = false
  listeners = [
    for server in var.app_servers : {
      port     = server.port
      protocol = "HTTP"
      default_action {
        fixed_response {
          content_type = "text/plain"
          content     = "Hello from ${server.name}"
          status_code = "200"
        }
      }
    }
  ]
}
    

In this example, the “for” expression is used to iterate over the “var.app_servers” list of objects and create a dynamic list of listener configurations for an AWS load balancer. This demonstrates the power of list and map interpolation for handling complex configurations.

Summary

List and map interpolation in HCL is a fundamental feature for creating dynamic and flexible Terraform configurations. By embedding lists and maps within your configurations, you can work with complex data structures, making it easier to define and manage your infrastructure as code.