TA-002-P · Question #235
Consider the following Terraform 0.12 configuration snippet: 1. variable "vpc_cidrs" { 2. type = map 3. default = { 4. us-east-1 = "10.0.0.0/16" 5. us-east-2 = "10.1.0.0/16" 6. us-west-1 =…
The correct answer is C. var.vpc_cidrs["us-east-1"]. Map variables in Terraform are accessed using var.<name>["<key>"] syntax, requiring the var. prefix and a string key in brackets.
Question
Options
- Avar.vpc_cidrs.0
- Bvpc_cidrs["us-east-1"]
- Cvar.vpc_cidrs["us-east-1"]
- Dvar.vpc_cidrs[0]
How the community answered
(17 responses)- A6% (1)
- C94% (16)
Why each option
Map variables in Terraform are accessed using `var.<name>["<key>"]` syntax, requiring the `var.` prefix and a string key in brackets.
`var.vpc_cidrs.0` uses numeric dot notation intended for list or tuple types, not map types that use string keys.
`vpc_cidrs["us-east-1"]` is missing the required `var.` prefix and would cause an unknown identifier error.
Map values in Terraform are accessed using bracket notation with the key as a string, so `var.vpc_cidrs["us-east-1"]` correctly retrieves the CIDR block for the `us-east-1` key. The `var.` prefix is mandatory to reference input variables, and the string key must match exactly as defined in the map.
`var.vpc_cidrs[0]` uses a numeric index which is invalid for a map type; maps require string keys, not integer positions.
Concept tested: Accessing map variable values in Terraform
Source: https://developer.hashicorp.com/terraform/language/expressions/types#maps-objects
Topics
Community Discussion
No community discussion yet for this question.