## TL;DR

You indexed a map with a key it does not have (`var.map["missing"]`). Unlike some languages, Terraform errors instead of returning null. Use `lookup(var.map, "missing", "default")` to provide a fallback, or restructure so the key always exists.

## The error

```text
Error: Missing map key

  on main.tf line 10, in output "secondary_location":
  10:   value = var.location["secondary"]

The given key does not identify an element in this collection value.
```

## Steps to fix

1. Confirm the key is genuinely absent vs misspelled: print the map with `terraform console` and evaluate `var.location`.
   - Expected: you see the actual key set.
2. If the key is optional, use `lookup` with a default:
   ```hcl
   value = lookup(var.location, "secondary", "East US")
   ```
   - Expected: missing keys fall back to the default instead of erroring.
3. If the key should always exist, fix the map (add the key to the variable default or the tfvars).
   - Expected: the key set is complete.
4. Re-run `terraform plan`.
   - Expected: plan proceeds.

## When to use this

- `plan` fails with `Missing map key` on bracket or dot access into a map, common with per-environment config maps and workspace-keyed locals.

## When NOT to use this

- `Invalid index` is the list/tuple equivalent. `Duplicate object key` is the for-expression collision. If the map itself is null, fix the null first.

## Compatibility

- All Terraform versions; `lookup` with a default is available since 0.12.

## Root cause

Map index operations in Terraform are strict: the key must exist. This is deliberate, catching typos and incomplete config at plan time rather than silently propagating nulls. `lookup()` is the explicit opt-out for keys that are legitimately optional.

## Edge cases

- `lookup` without a default still errors on missing keys; the default is what makes it safe.
- `try(var.map["key"], "default")` also works and handles null maps.
- In `validation` blocks, wrap the access in `can()` so a missing key fails validation gracefully instead of erroring.