# Terraform: Error: Invalid index — The given key does not identify an element in this collection value

**Code:** `Invalid index: The given key does not identify an element` · **Category:** Terraform · **Seen on:** Terraform

You indexed a map/list/set with a key or position that doesn't exist.

## Meaning

Typical: `var.map["key"]` missing, `resource.x[0]` when `count = 0`, `aws_instance.web[*].id[0]` on empty, or `for_each` keys changed. Use `lookup()`, `try()`, `one()`, or check `length()`. With `count = 0` resources, use `one(resource[*].id)` to get null/the single value.

## Common causes

- Map key missing
- Empty list indexed `[0]`
- Resource with `count = 0` referenced by index
- for_each key not present
- Output of conditional resource

## Quick fix

1. `one(aws_x.this[*].id)` for 0-or-1 resources
2. `lookup(map, key, default)`
3. `try(local.list[0], null)`

## Detailed fix by platform

### Terraform

1. `output "id" { value = one(aws_instance.optional[*].id) }`

## How to diagnose

1. **Collection** — Empty?
2. **Key** — Exists?
3. **Pattern** — one()/try()?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/terraform/invalid-index-key-does-not-identify-an-element)
