nerdexam
HashiCorp

TERRAFORM-ASSOCIATE-004 · Question #261

Consider the following Terraform 0.12 configuration snippet: variable "vpc_cidrs" { type = map default = { us-east-1 = "10.0.0.0/16" us-east-2 = "10.1.0.0/16" us-west-1 = "10.2.0.0/16" us-west-2 = "10

The correct answer is A. var.vpc_cidrs["us-east-1"]. To access a specific value from a map variable in Terraform, you use the var. prefix followed by the variable name and the key in square brackets.

Submitted by emma.c· Apr 18, 2026Work with Terraform Configurations

Question

Consider the following Terraform 0.12 configuration snippet: variable "vpc_cidrs" { type = map default = { us-east-1 = "10.0.0.0/16" us-east-2 = "10.1.0.0/16" us-west-1 = "10.2.0.0/16" us-west-2 = "10.3.0.0/16" } } resource "aws_vpc" "shared" { cidr_block = _____________ } How would you define the cidr_block for us-east-1 in the aws_vpc resource using a variable?

Options

  • Avar.vpc_cidrs["us-east-1"]
  • Bvar.vpc_cidrs.0
  • Cvpc_cidrs["us-east-1"]
  • Dvar.vpc_cidrs[0]

How the community answered

(54 responses)
  • A
    81% (44)
  • B
    6% (3)
  • C
    11% (6)
  • D
    2% (1)

Why each option

To access a specific value from a map variable in Terraform, you use the `var.` prefix followed by the variable name and the key in square brackets.

Avar.vpc_cidrs["us-east-1"]Correct

To access a specific value within a map variable named `vpc_cidrs`, the correct syntax uses `var.<variable_name>["<key>"]`, making `var.vpc_cidrs["us-east-1"]` the appropriate way to retrieve the CIDR block for that region.

Bvar.vpc_cidrs.0

Accessing map elements by numerical index (e.g., `.0`) is not valid for Terraform map types, which require string keys.

Cvpc_cidrs["us-east-1"]

Terraform variable references must be prefixed with `var.`, so `vpc_cidrs["us-east-1"]` without the prefix is incorrect syntax.

Dvar.vpc_cidrs[0]

Accessing map elements by numerical index (e.g., `[0]`) is not valid for Terraform map types, which require string keys.

Concept tested: Accessing Terraform map variable elements

Source: https://developer.hashicorp.com/terraform/language/values/variables#accessing-input-variable-values

Topics

#Variables#Map Type#Terraform Syntax#Resource Configuration

Community Discussion

No community discussion yet for this question.

Full TERRAFORM-ASSOCIATE-004 Practice