I’m getting error, when trying to reference my subnet ids for route table association.
Blockquote#
Public Subnet
resource “aws_subnet” “public-subnet” {
for_each = var.public_subnet_cidr_map_of_maps
vpc_id = [for my_vpc in aws_vpc.vpc_network_map : my_vpc.id if my_vpc.tags.Name == “Management-VPC”][0]
availability_zone = each.value.availability_zone
cidr_block = each.value.cidr
tags = {
Name = each.value.name
}
}
#using map to iterate
output J_vpc_list {
value = {for my_vpc in aws_subnet.public-subnet : my_vpc.tags.Name => my_vpc.id}
}
#doing associtaion:
#Assign the public route table to the public Subnet
resource “aws_route_table_association” “public-rt-association” {
for_each = {for my_vpc in aws_subnet.public-subnet : my_vpc.tags.Name => my_vpc.id}
subnet_id = aws_subnet.public-subnet[each.key]
route_table_id = aws_route_table.public-rt.id
}
Blockquote
I’m getting following error:
Blockquote
Error: Invalid index
on network.tf line 125, in resource “aws_route_table_association” “public-rt-association”:
125: subnet_id = aws_subnet.public-subnet[each.value]
|----------------
| aws_subnet.public-subnet is object with 3 attributes
| each.value is “subnet-01d53d016846ac98c”
The given key does not identify an element in this collection value.
Error: Invalid index
on network.tf line 125, in resource “aws_route_table_association” “public-rt-association”:
125: subnet_id = aws_subnet.public-subnet[each.value]
|----------------
| aws_subnet.public-subnet is object with 3 attributes
| each.value is “subnet-04781855551cf83f9”
The given key does not identify an element in this collection value.
Error: Invalid index
on network.tf line 125, in resource “aws_route_table_association” “public-rt-association”:
125: subnet_id = aws_subnet.public-subnet[each.value]
|----------------
| aws_subnet.public-subnet is object with 3 attributes
| each.value is “subnet-0e65784f2b1b0f719”
The given key does not identify an element in this collection value.
Blockquote
Can you please explain what I’m doing wrong.
Also, what if I want only subnet id 2 to be associated to route table? How can I do this?