Is there a way to get the attribute value from a map within a map?
variable "vpc" {
type = map(object({
vpc_cidr_block = string
instance_tenancy = optional(string, null)
enable_dns_support = optional(bool, null)
enable_dns_hostnames = optional(bool, null)
assign_generated_ipv6_cidr_block = optional(bool, null)
tags = optional(map(string), { "created_by" : "terraform" })
subnets = map(object({
subnet_cidr_block = string
}))
}))
description = "Map variable to create a new VPC"
}
resource "aws_subnet" "this" {
for_each = var.vpc
vpc_id = aws_vpc.this[each.key].id
cidr_block = each.value.subnets[each.key].subnet_cidr_block
}
I want to get the attribute value from the subnet_cidr_block
It’s giving me this error
│ Error: Invalid index
│
│ on ../../../modules/aws/vpc/main.tf line 15, in resource "aws_subnet" "this":
│ 15: cidr_block = each.value.subnets[each.key].subnet_cidr_block
│ ├────────────────
│ │ each.key is "test"
│ │ each.value.subnets is map of object with 1 element
│
│ The given key does not identify an element in this collection value.
I can’t find anything to resolve my problem!
Thanks in advance guys