我有 2 个前端和后端子网,目前我将子网范围作为变量传递,但是有没有办法通过 terraform 在 VPC 中选择可用 cidr 范围?
Hashicorp 有一个模块可用于帮助子网 CIDR https://registry.terraform.io/modules/hashicorp/subnets/cidr/1.0.0
module "subnet_addrs" {
source = "hashicorp/subnets/cidr"
base_cidr_block = "10.0.0.0/8"
networks = [
{
name = "foo"
new_bits = 8
},
{
name = "bar"
new_bits = 8
},
{
name = "baz"
new_bits = 4
},
{
name = "beep"
new_bits = 8
},
{
name = "boop"
new_bits = 8
},
]
}
这将提供带有值的network_cidr_blocks
输出
{
foo = "10.0.0.0/16"
bar = "10.1.0.0/16"
baz = "10.16.0.0/12"
beep = "10.32.0.0/16"
boop = "10.33.0.0/16"
}
您也可以使用一些内置函数自行从 VPC CIDR 中计算,cidrsubnets
和cidrsubnet
https://www.terraform.io/docs/configuration/functions/cidrsubnets.html
> cidrsubnets("10.0.0.0/8", 8, 8, 4, 8, 8)
[
"10.0.0.0/16",
"10.1.0.0/16",
"10.16.0.0/12",
"10.32.0.0/16",
"10.33.0.0/16",
]