Skip to content

Instantly share code, notes, and snippets.

@mhorbul
Last active June 7, 2022 15:23
Show Gist options
  • Save mhorbul/12d7ea6401bf7ad67cfab0207e463750 to your computer and use it in GitHub Desktop.
Save mhorbul/12d7ea6401bf7ad67cfab0207e463750 to your computer and use it in GitHub Desktop.
locals {
vpc_cidr = "172.31.0.0/16"
region = "us-east-1"
azs = ["a", "b", "c"]
subnets = {
"private" = {
all = 2,
az = 2,
},
"public" = {
all = 4,
az = 2,
},
"database" = {
all = 6,
az = 2
},
"intra" = {
all = 6
az = 2
},
}
cidrs = cidrsubnets(local.vpc_cidr, [for k, v in local.subnets : v.all]...)
network_size = {
"255.255.0.0" = 65534, # /16
"255.255.192.0" = 16382, # /18
"255.255.240.0" = 4094, # /20
"255.255.252.0" = 1022, # /22
"255.255.254.0" = 510, # /23
"255.255.255.0" = 254, # /24
"255.255.255.192" = 62, # /26
}
}
output "subnets" {
value = {
"${local.vpc_cidr} (${local.network_size[cidrnetmask(local.vpc_cidr)]} ips)" = {
for n in keys(local.subnets) : n => {
"${local.cidrs[index(keys(local.subnets), n)]} (${local.network_size[cidrnetmask(local.cidrs[index(keys(local.subnets), n)])]} ips)" = {
for sidx, s in cidrsubnets(local.cidrs[index(keys(local.subnets), n)], [for aidx, i in toset(local.azs) : local.subnets[n].az]...) : "${local.region}${local.azs[sidx]}" => "${s} (${local.network_size[cidrnetmask(s)]} ips)"
}
}
}
}
}
# $ terraform plan
#
# + subnets = {
# + 172.31.0.0/16 (65534 ips) = {
# + database = {
# + 172.31.0.0/22 (1022 ips) = {
# + us-east-1a = "172.31.0.0/24 (254 ips)"
# + us-east-1b = "172.31.1.0/24 (254 ips)"
# + us-east-1c = "172.31.2.0/24 (254 ips)"
# }
# }
# + intra = {
# + 172.31.4.0/22 (1022 ips) = {
# + us-east-1a = "172.31.4.0/24 (254 ips)"
# + us-east-1b = "172.31.5.0/24 (254 ips)"
# + us-east-1c = "172.31.6.0/24 (254 ips)"
# }
# }
# + private = {
# + 172.31.64.0/18 (16382 ips) = {
# + us-east-1a = "172.31.64.0/20 (4094 ips)"
# + us-east-1b = "172.31.80.0/20 (4094 ips)"
# + us-east-1c = "172.31.96.0/20 (4094 ips)"
# }
# }
# + public = {
# + 172.31.128.0/20 (4094 ips) = {
# + us-east-1a = "172.31.128.0/22 (1022 ips)"
# + us-east-1b = "172.31.132.0/22 (1022 ips)"
# + us-east-1c = "172.31.136.0/22 (1022 ips)"
# }
# }
# }
# }
@unacceptable
Copy link

Excellent work! Where do you find the expansion documentation for []...?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment