How to associate routes to gateway vpc endpoints

I am trying to write a module that creates vpc gateway endpoints based on input variable (s3, and/or dynamo). I am using a for_each to get the list of route table IDs and create thr route associations with the vpc endpoints. I tried using count .index to iterate through vpcendpoints but it doesn’t work with for-each. Here’s the non-working code:

variable “vpc_gateway_endpoints” {
type = list(string)
default = [
“com.amazonaws.us-west-2.s3”,
“com.amazonaws.us-west-2.dynamodb”,
]
}

resource “aws_vpc_endpoint” “vpc_endpoint” {
vpc_id = data.aws_vpc.vpc.id

count = length(var.vpc_gateway_endpoints)
service_name = var.vpc_gateway_endpoints[count.index]
vpc_endpoint_type = “Gateway”
private_dns_enabled = true
policy = var.policy
}

resource “aws_vpc_endpoint_route_table_association” “private_route” {
count = length(var.vpc_gateway_endpoints)

for_each = data.aws_route_tables.rts.ids
route_table_id = each.value

vpc_endpoint_id = aws_vpc_endpoint.vpc_endpoint[count.index].id
}

It seems like I just need to have a nested loop but not sure how. Any help would be greatly appreciated.

How about this?
The VPC is created with the official VPC module.

resource "aws_vpc_endpoint" "gateway" {
  vpc_endpoint_type = "Gateway"
  vpc_id            = module.vpc.vpc_id
  for_each          = local.vpc_gateway_endpoints.service_name
  service_name      = each.value
  route_table_ids   = local.all_vpc_subnet_ids
}

locals {
  all_vpc_subnet_ids     = flatten([module.vpc.public_route_table_ids, module.vpc.private_route_table_ids])
  vpc_gateway_endpoints = {
    service_name = {
      s3       = join(".", ["com.amazonaws", data.aws_region.current.name, "s3"]),
      dynamodb = join(".", ["com.amazonaws", data.aws_region.current.name, "dynamodb"])
    }
  }
}