Error fetching route_table_id from a data block

I am trying to fetch route table ids and running into some issues. any help would be appreciated.

data "aws_subnet_ids" "private" {
  vpc_id = data.aws_vpc.selected.id

  filter {
    name   = "tag:Name"
    values = ["*.private.xxx.xxxx.xxxx.com"]
  }
}
data "aws_route_table" "selected" {
  for_each = data.aws_subnet_ids.private.ids
  subnet_id = each.value
}
resource "aws_route" "private_connection" {
  for_each = {
  for pair in setproduct(
  [for name, subnet in var.private_subnets : name],
  [for peer in var.peers : peer.vpc_id]
  ) : "${pair[0]}-${pair[1]}" => { net = pair[0], id = pair[1] } }
  route_table_id            = data.aws_route_table.selected[each.key]
  destination_cidr_block    = data.aws_vpc_peering_connection.peer[each.value.id].peer_cidr_block
  vpc_peering_connection_id = aws_vpc_peering_connection.peer[each.value.id].id
}

The error i am getting is -

│ Error: Invalid index
│
│  128:   route_table_id            = data.aws_route_table.selected[each.key]
│     ├────────────────
│     │ data.aws_route_table.selected is object with 2 attributes

Hi @rose!

It looks like you’ve truncated that error message a bit. I would’ve expected to also see a value for each.key and then a paragraph explaining more about the error. The full error message is all of the lines starting with the character in the output.

Can you share the full error message, just so I can make sure I’m understanding properly what problem you’ve encountered? Thanks!