Interview Prep/Terraform/Loops, Conditionals, Dynamic Blocks and Functions

How do you safely read a map key that may not exist?

IntermediateConceptLoops, Conditionals, Dynamic Blocks and Functions
Answer

lookup(map, "key", default) or try(map["key"], default). Direct indexing with a missing key fails: Invalid index: the given key does not identify an element in this collection value. For nested optional attributes try(var.cfg.a.b, null) is common.

All Terraform questions Roadmap: Terraform

More on Loops, Conditionals, Dynamic Blocks and Functions