首页 文章

Terraform在多个VPC下创建子网

提问于
浏览
0

我在Terraform面临一个问题 . 我试图在我的变量文件中使用预定义的CIDR范围来配置VPC和子网 . 下面是变量文件的代码片段,即variables.tf

variable "vpc-fullcidr" {
  description = "VPC CIDR range"
  type = "list"
  default = ["10.0.0.0/16", "192.168.0.0/16"]
}
variable "subnet-cidr" {
  description = "Subnet CIDR range"
  type = "list"
  default = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24", "192.168.0.0/24", 
  "192.168.1.0/24", "192.168.2.0/24"]
}

variable "vpc-count" {
  description = "Number of VPC to create"
  type    = "string"
  default = "2"
}

variable "subnet-count"
{
  description = "Number of Subnets to create"
  default = "6"
}

以下是我的VPC模板 . VPC的配置没有任何问题: -

resource "aws_vpc" "VPCPrimary" {
   count = "${var.vpc-count}"
   cidr_block = "${element(var.vpc-fullcidr, count.index)}"
   #### Below DNS values are for the internal vpc dns resolution
   enable_dns_support = true
   enable_dns_hostnames = true
   tags {
     Name = "${element(var.VPCNameTag, count.index)}"
   }
}

当我尝试创建子网时,它失败了 . 以下是我的子网模板: -

resource "aws_subnet" "PrivateSubnets" {
  count = "${var.subnet-count}"
  vpc_id = "${element(aws_vpc.VPCPrimary.*.id, count.index)}"
  availability_zone = "${var.primaryaz}"
  cidr_block        = "${element(var.subnet-cidr, count.index)}"
  tags {
  Name = "Private Subnet ${count.index + 1}"
  }
}

Terraform错误日志: -

Error: Error applying plan:

2 error(s) occurred:

* aws_subnet.PrivateSubnets[4]: 1 error(s) occurred:

* aws_subnet.PrivateSubnets.4: Error creating subnet: InvalidSubnet.Range: 
The CIDR '192.168.1.0/24' is invalid.
status code: 400, request id: 1a9136e6-8631-43cb-99a6-ccd2522854d4
* aws_subnet.PrivateSubnets[1]: 1 error(s) occurred:

* aws_subnet.PrivateSubnets.1: Error creating subnet: InvalidSubnet.Range: 
The CIDR '10.0.2.0/24' is invalid.
status code: 400, request id: 1b0e4cf5-2e1d-402e-a89d-f08d8ac694e1

我正在尝试以自动方式配置基础结构,其中使用存储过程使用.NET前端中的用户输入动态填充variables.tf文件 . 这个问题是当我尝试创建多个VPC以及多个子网时 . 有什么方法可以在子架资源“aws_subnet”中将子网CIDR块与VPC CIDR块映射 .

1 回答

  • 1

    您定义了两个VPC,我将其称为 AB

    default = ["10.0.0.0/16", "192.168.0.0/16"]
    #             A               B
    

    然后定义了6个子网:

    default = [
        "10.0.1.0/24", 
        "10.0.2.0/24",
        "10.0.3.0/24",
        "192.168.0.0/24", 
        "192.168.1.0/24",
        "192.168.2.0/24"]
    

    创建子网时,循环子网资源6次,每个子网一次:

    resource "aws_subnet" "PrivateSubnets" {
      count = "${var.subnet-count}"
      vpc_id = "${element(aws_vpc.VPCPrimary.*.id, count.index)}"
    

    在上面,您还使用计数来索引创建的VPC . 这个索引从1-6开始,而你只有2个VPC,所以它包装 . 这意味着当它通过子网1-6时,它将重复VPC作为ABABAB . 如果将该模式应用于您定义的6,您可以看到为什么2失败:

    default = [
        "10.0.1.0/24",      #A
        "10.0.2.0/24",      #B <- this fails
        "10.0.3.0/24",      #A
        "192.168.0.0/24",   #B
        "192.168.1.0/24",   #A <- this fails
        "192.168.2.0/24"]   #B
    

    您现在可以看到为什么其中两个失败,因为它们是在错误的VPC中创建的 . 一个简单的解决方法是只对子网进行排序,以便它们正确地索引到VPC中:

    default = [
        "10.0.1.0/24",     #A
        "192.168.0.0/24",  #B
        "10.0.2.0/24",     #A
        "192.168.1.0/24",  #B
        "10.0.3.0/24",     #A
        "192.168.2.0/24"]  #B
    

    对于更通用的解决方案,您可能希望研究使用模块,您可以将模块中的单个VPC CIDR和相关子网多次提供给模块 .

相关问题