Query on reducing iteration of specific resources

I am trying to reduce iteration in some code for sizing vpc’s the fundamentals work
Based on the cidr you supply for example 10.179.0.0/22, I determine the trailing 2 chrs in locals then look them up from a map to create logic. The lookup here would be var.cidrs 22 = medium etc etc

locals {
 ....
  size        = substr(var.cidr_block, -2, 2)
  subnet_size = lookup(var.cidrs, local.size)
  subnet_cidrs_m  = cidrsubnets(var.cidr_block, 3, 3, 3, 3, 3, 5, 5, 5, 7, 7, 7, 5, 5, 5, 5)

for example

resource "aws_route_table_association" "private_subnet_m" {
  count          = local.subnet_size == "medium" ? length(local.subnet_cidrs_m) : 0
  route_table_id = aws_default_route_table.private_route.id
  subnet_id      = element(aws_subnet.private_subnet_m.*.id , count.index)
}

But the issue I run into is now when creating thing like endpoints the logic is very repetative
as the outcome could be private_subnet_s / m / l. Which means that this can differ depending on the logic above, as I want to just pick the last three subnets in any outcome

for example , depending on the subnet this varies so creating endpoints need 3x etc

subnet_ids   = [aws_subnet.private_subnet_m[7].id,aws_subnet.private_subnet_m[8].id,aws_subnet.private_subnet_m[9].id]

Would appreciate any thoughts on improvements

This was what I came up with seems to work during plan,

subnet_ids   = ["aws_subnet.private_subnet_${substr(local.subnet_size, 0, 1)}[7].id","aws_subnet.private_subnet_${substr(local.subnet_size, 0, 1)}[8].id","aws_subnet.private_subnet_${substr(local.subnet_size, 0, 1)}[9].id"]
      + subnet_ids            = [
          + "aws_subnet.private_subnet_m[7].id",
          + "aws_subnet.private_subnet_m[8].id",
          + "aws_subnet.private_subnet_m[9].id",

however on execution I get InvalidSubnetId.Malformed: Invalid Subnet Id: ‘aws_subnet.private_subnet_m[7].id’ (expecting ‘subnet-…; the Id may only contain lowercase alphanumeric characters and a single dash’)

Hi All, I think this is sorted now, the through peer assistance. This keeps the resource the same name for all variations of cidr size.

subnet_cidrs = lookup({ 21 = cidrsubnets(var.cidr_block, 3, 3, …), 22 = cidrsubnets(var.cidr_block, 4, 4,…), 23 = cidrsubnets(var.cidr_block, 4, 4, …)}, local.size )