如何使用 cidrsubnets 将子网的 CIDR 范围传递给 Terraform 版本 0.14 中的 VPC 模块
How to pass CIDR range for subnets using cidrsubnets to VPC module in Terraform Version 0.14
Terraform cidrsubnets 为我提供了以下子网,这正是我想要的。
我使用的 Terraform 版本:Terraform v0.14.9
> cidrsubnets("172.16.0.0/18", 6, 6, 6, 6)
tolist([
"172.16.0.0/24",
"172.16.1.0/24",
"172.16.2.0/24",
"172.16.3.0/24",
])
Requirement
==========
Main CIDR range = 172.16.0.0/18
Public Subnets = [172.16.0.0/24, 172.16.1.0/24]
Private Subents = [172.16.2.0/24, 172.16.3.0/24]
如何使用 public 和私有子网传递上述 CIDR 范围
以下 VPC 模块的 Terraform cidrsubnets 函数。
variable "vpc_cidr" {
default = "172.16.0.0/18"
}
data "aws_availability_zones" "azs" {
state = "available"
}
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "2.77.0"
name = "my-vpc"
cidr = var.vpc_cidr
azs = data.aws_availability_zones.azs.names
private_subnets = ["172.16.1.0/24", "172.16.2.0/24"] <= I want to pass these subnets from cidrsubnets function
public_subnets = ["172.16.3.0/24", "172.16.4.0/24"] <= I want to pass these subnets from cidrsubnets function
enable_nat_gateway = true
single_nat_gateway = true
enable_dns_hostnames = true
}
有人可以帮我解决这个问题吗?
您可以使用 slice function on top of the cidrsubnets 一个。由于 cidrsubnets
将 return 一个列表,根据索引将其切片,您将有 2 个列表,其中包含 public 和请求的私有 IP:
module "vpc" {
...
private_subnets = slice(cidrsubnets(var.vpc_cidr, 6, 6, 6, 6),0,2)
public_subnets = slice(cidrsubnets(var.vpc_cidr, 6, 6, 6, 6),2,4)
...
}
Terraform cidrsubnets 为我提供了以下子网,这正是我想要的。
我使用的 Terraform 版本:Terraform v0.14.9
> cidrsubnets("172.16.0.0/18", 6, 6, 6, 6)
tolist([
"172.16.0.0/24",
"172.16.1.0/24",
"172.16.2.0/24",
"172.16.3.0/24",
])
Requirement
==========
Main CIDR range = 172.16.0.0/18
Public Subnets = [172.16.0.0/24, 172.16.1.0/24]
Private Subents = [172.16.2.0/24, 172.16.3.0/24]
如何使用 public 和私有子网传递上述 CIDR 范围 以下 VPC 模块的 Terraform cidrsubnets 函数。
variable "vpc_cidr" {
default = "172.16.0.0/18"
}
data "aws_availability_zones" "azs" {
state = "available"
}
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "2.77.0"
name = "my-vpc"
cidr = var.vpc_cidr
azs = data.aws_availability_zones.azs.names
private_subnets = ["172.16.1.0/24", "172.16.2.0/24"] <= I want to pass these subnets from cidrsubnets function
public_subnets = ["172.16.3.0/24", "172.16.4.0/24"] <= I want to pass these subnets from cidrsubnets function
enable_nat_gateway = true
single_nat_gateway = true
enable_dns_hostnames = true
}
有人可以帮我解决这个问题吗?
您可以使用 slice function on top of the cidrsubnets 一个。由于 cidrsubnets
将 return 一个列表,根据索引将其切片,您将有 2 个列表,其中包含 public 和请求的私有 IP:
module "vpc" {
...
private_subnets = slice(cidrsubnets(var.vpc_cidr, 6, 6, 6, 6),0,2)
public_subnets = slice(cidrsubnets(var.vpc_cidr, 6, 6, 6, 6),2,4)
...
}