DEV Community

1suleyman
1suleyman

Posted on

💻 How to Reference Values in Terraform Lists & Maps (And Why It’s Like Picking Items by Name or Number)

Hey everyone 👋

If you're learning Terraform — whether for certification, work, or fun — there’s one skill you’ll definitely use over and over again:

Accessing values inside variables — especially when they’re stored in a list or a map.

I didn’t get it at first. I kept asking:

“Do I need a key? An index? Brackets? Curly braces?”

So in this post, I’ll break it down like I wish someone had done for me. Let's talk about how to pull specific values from lists and maps in Terraform — and why it's way easier than it sounds 👇


🧸 Think of It Like a Warehouse: Named Drawers vs Numbered Shelves

Imagine you’re in a warehouse. You have two ways to find items:

  • ✅ Ask for something by name → “Get me the tools from the ‘hardware’ drawer.”
  • ✅ Ask for something by position → “Get me the second item on the third shelf.”

That’s exactly how Terraform handles:

  • 🗺️ Maps — values you access using a key (like a labeled drawer)
  • 📋 Lists — values you access using a numbered index (like a shelf position)

🔑 Accessing Values from a Map (The "Labeled Drawer" Approach)

Let’s say you have a map of AWS regions and their corresponding EC2 instance types:

variable "instance_types" {
  type = map(string)
  default = {
    "us-west-2"  = "t2.nano"
    "ap-south-1" = "t2.small"
  }
}
Enter fullscreen mode Exit fullscreen mode

If you want to deploy to "ap-south-1", just reference the key:

instance_type = var.instance_types["ap-south-1"]
Enter fullscreen mode Exit fullscreen mode

🧠 This returns "t2.small".

💡 It’s like saying: “Open the drawer labeled ‘ap-south-1’ and give me what’s inside.”

You can even change the key dynamically, and Terraform will pick the right value for that region!


📋 Accessing Values from a List (The "Shelf Position" Method)

Now let’s say you have a list of instance types:

variable "sizes" {
  type = list(string)
  default = ["m5.large", "m5.xlarge", "m5.2xlarge"]
}
Enter fullscreen mode Exit fullscreen mode

To get the first item (index 0), use:

instance_type = var.sizes[0]
Enter fullscreen mode Exit fullscreen mode

💡 It’s like saying: “Grab the first box from the shelf.”

Index Value
0 "m5.large"
1 "m5.xlarge"
2 "m5.2xlarge"

Change the index, change the value Terraform uses. Easy.


🧪 When You’ll Use This in the Real World

This pattern comes in handy a lot — like when you're:

  • Deploying different resources based on region
  • Dynamically referencing tags, settings, or IP addresses
  • Scaling infrastructure by picking from a list of configurations

💼 Real Example

variable "env_tags" {
  type = map(string)
  default = {
    "Name" = "backend-app"
    "Team" = "engineering"
  }
}

resource "aws_instance" "app" {
  ami           = "ami-123456"
  instance_type = "t3.micro"
  tags          = var.env_tags
}
Enter fullscreen mode Exit fullscreen mode

You can also extract just one tag value:

output "team_name" {
  value = var.env_tags["Team"]
}
Enter fullscreen mode Exit fullscreen mode

🧠 Pro Tips to Remember

Concept Syntax Analogy
Get value from map var.map["key"] Ask for item in a labeled drawer
Get value from list var.list[0] Pick item from a specific shelf
List starts at index 0 ["A", "B", "C"] → A = [0] Like counting from the first slot
You must use brackets Maps: ["key"], Lists: [0] Don’t mix them up!
Indexing wrong? Error! index out of range or mismatch Like asking for a drawer that doesn’t exist 🧨

✅ Final Thoughts

Referencing values from lists and maps in Terraform might look scary at first, but once you see it like a warehouse system of labels and positions, it becomes second nature.

And the best part? This isn’t just for passing the Terraform Associate exam — it’s for real-world infrastructure too:

  • Tagging resources
  • Setting region-specific configs
  • Reusing lists of instance types or security groups

If this post helped clarify things for you, let me know on LinkedIn or leave a comment! I’m learning Terraform just like you — one bracket at a time 😉

Stay curious, stay cloud-native ☁️💪

Top comments (0)