Course contentsShow
← Cloud and Infrastructure
Lesson 734 of 2,98914. Terraform FundamentalsFree lesson

Lists and Maps in HCL

Creating and using list and map data structures for organizing configuration values.

Lists and Maps in HCL

You'll learn how to organize multiple configuration values using lists and maps in Terraform's HCL syntax.

What Are Lists and Maps?

Think of lists and maps as containers for organizing related data. A list is like a numbered grocery list—an ordered collection of items you access by position. A map is like a dictionary—a collection of key-value pairs where you look up values by their descriptive names.

These data structures help you avoid repeating similar values throughout your configuration and make your infrastructure code cleaner and more maintainable.

Lists: Ordered Collections

A list groups values in a specific order, enclosed in square brackets:

variable "availability_zones" {
  type    = list(string)
  default = ["us-east-1a", "us-east-1b", "us-east-1c"]
}

Access list items by their zero-based index: var.availability_zones[0] returns "us-east-1a".

Maps: Key-Value Pairs

A map associates descriptive keys with values, enclosed in curly braces:

variable "instance_types" {
  type = map(string)
  default = {
    dev  = "t2.micro"
    prod = "t3.large"
  }
}

Access map values by key: var.instance_types["dev"] returns "t2.micro".

When to Use Each

Use lists when order matters or you have similar items without meaningful names—like multiple subnet IDs or availability zones. Use maps when you need descriptive labels—like environment-specific settings or tag values.

Key Takeaway: Lists and maps in HCL organize multiple configuration values into reusable, structured collections—lists for ordered sequences and maps for named key-value pairs.