Hi all, im fairly new to terraform and im trying to create a subnet model where i recieve the name and subnet mask.
Based of the subnet mask i generate a list of CIDRs using “cidrsubnets”. I would like to merge based of the index, the map of subnets to include subnet_name, subnet_size and the list of subnet_cidr
Expected outcome is
[
{
subnet_ip = "10.0.0.0/26"
subnet_name = "app"
subnet_size = 26
},
{
subnet_ip = "10.0.0.64/27"
subnet_name = "data"
subnet_size = 27
},
{
subnet_ip = "10.0.0.96/26"
subnet_name = "shd"
subnet_size = 27
}
]
locals {
base_cidr_block = "10.0.0.0/24"
cidr_mask = tonumber(split("/", local.base_cidr_block)[1])
#This will be a tfvar input
subnet_input = {
"app" = 26
"data" = 27
}
#this is static, always part of subnetting
shd_subnet = {
"shd" = 27
}
subnet_merge = merge(local.subnet_input, local.shd_subnet)
subnet_cidr = cidrsubnets(local.base_cidr_block, [for k,v in local.subnet_merge : v - local.cidr_mask]...)
first_try = concat(
[
for i in range(length(local.subnet_cidr)) :
[ for k,v in local.subnet_merge : {
subnet_name = k
subnet_size = v
subnet_ip = local.subnet_cidr[i]
}]])
second_try = merge(flatten(
[
for i in range(length(local.subnet_cidr)) : [
for k,v in local.subnet_merge : {
subnet_name = k
subnet_size = v
subnet_ip = local.subnet_cidr[i]
}]])...)
}
output "subnet_merge" {
value = local.subnet_merge
}
output "subnet_list" {
value = local.subnet_cidr
}
output "first_try" {
value = local.first_try
}
output "second_try" {
value = local.second_try
}