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.