TA-002-P · Question #235
TA-002-P Question #235: Real Exam Question with Answer & Explanation
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
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 = "10.2.0.0/16" 7. us-west-2 = "10.3.0.0/16" 8. } 9. } 10. 11. resource "aws_vpc" "shared" { 12. cidr_block = _____________ 13. } How would you define the cidr_block for us-east-1 in the aws_vpc resource using a variable?
Options
- Avar.vpc_cidrs.0
- Bvpc_cidrs["us-east-1"]
- Cvar.vpc_cidrs["us-east-1"]
- Dvar.vpc_cidrs[0]
Explanation
Map variables in Terraform are accessed using var.<name>["<key>"] syntax, requiring the var. prefix and a string key in brackets.
Common mistakes.
- A.
var.vpc_cidrs.0uses numeric dot notation intended for list or tuple types, not map types that use string keys. - B.
vpc_cidrs["us-east-1"]is missing the requiredvar.prefix and would cause an unknown identifier error. - D.
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
Reference. https://developer.hashicorp.com/terraform/language/expressions/types#maps-objects
Topics
Community Discussion
No community discussion yet for this question.