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