繁体   English   中英

在 Terraform 中创建 VPC 和相应的子网

[英]VPC and corresponding subnets creation in Terraform

我必须创建 4 个 VPC,将 1 个子网附加到前 3 个 VPC; 但最后一个 VPC 将有 3 个子网。

它抛出错误,原因是当它到达为第四个 VPC 创建子网时,它还有另外两个 CIDR .. 请帮忙。

    resource "aws_subnet" "subnet-assign" {
    vpc_id = "{aws_vpc.External_VPC.*.id}"
     count = length(var.subnet_cidr)
      #cidr_block = element(var.subnet_cidr,count.index)
       #for_each = {for idx,cidr_block in var.subnet_cidr: cidr_block=> idx}
          #cidr_block = each.key
             cidr_block = element(concat(var.subnet_cidr, [""]), count.index)

}

允许在模块上使用for_each的新功能对于这样的东西来说非常重要。 制作一个模块(我称之为 vpc)。 然后在模块上 for_each。 制作一个复杂的数据结构来描述您的网络。 这需要 terraform 0.13+

这是我的目录内容。 该文件夹是我制作的vpc模块。

.
├── main.tf
├── terraform.tfstate
└── vpc
    └── main.tf

1 directory, 3 files

这是 vpc 模块的内容:

variable name {
    type = string
}
variable vpc_mapping {
    type = object({
        cidr_block = string
        subnets = map(object({
            cidr_block = string
        }))
    })
}

resource "aws_vpc" "default" {
  cidr_block       = var.vpc_mapping.cidr_block
  instance_tenancy = "default"
}

resource "aws_subnet" "main" {
  for_each = var.vpc_mapping.subnets
  vpc_id     = aws_vpc.default.id
  cidr_block = each.value.cidr_block
}

这是我调用模块的根 terraform 代码的内容。

provider aws {
    profile = "myprofile"
    region = "us-west-2"
}

locals {
    mapping = map(
        "one", {
            cidr_block = "10.1.0.0/16"
            subnets = map(
                "one", {
                    cidr_block = "10.1.0.0/24"
                },
            )
        },
        "two", {
            cidr_block = "10.2.0.0/16"
            subnets = map(
                "one", {
                    cidr_block = "10.2.0.0/24"
                },
            )
        },
        "three", {
            cidr_block = "10.3.0.0/16"
            subnets = map(
                "one", {
                    cidr_block = "10.3.0.0/24"
                },
            )
        },
        "four", {
            cidr_block = "10.4.0.0/16"
            subnets = map(
                "one", {
                    cidr_block = "10.4.0.0/24"
                },
                "two", {
                    cidr_block = "10.4.1.0/24"
                },
                "three", {
                    cidr_block = "10.4.2.0/24"
                },
            )
        },
    )
}

module vpcs {
    source = "./vpc"

    for_each = local.mapping

    name = each.key
    vpc_mapping = each.value
}

这是计划输出:

Terraform will perform the following actions:

  # module.vpcs["four"].aws_subnet.main["one"] will be created
  + resource "aws_subnet" "main" {
      + vpc_id                          = (known after apply)
      + assign_ipv6_address_on_creation = false
      + cidr_block                      = "10.4.0.0/24"
      + map_public_ip_on_launch         = false
    }

  # module.vpcs["four"].aws_subnet.main["three"] will be created
  + resource "aws_subnet" "main" {
      + vpc_id                          = (known after apply)
      + assign_ipv6_address_on_creation = false
      + cidr_block                      = "10.4.2.0/24"
      + map_public_ip_on_launch         = false
    }

  # module.vpcs["four"].aws_subnet.main["two"] will be created
  + resource "aws_subnet" "main" {
      + vpc_id                          = (known after apply)
      + assign_ipv6_address_on_creation = false
      + cidr_block                      = "10.4.1.0/24"
      + map_public_ip_on_launch         = false
    }

  # module.vpcs["four"].aws_vpc.default will be created
  + resource "aws_vpc" "default" {
      + id                               = (known after apply)
      + assign_generated_ipv6_cidr_block = false
      + cidr_block                       = "10.4.0.0/16"
      + enable_dns_support               = true
      + instance_tenancy                 = "default"
    }

  # module.vpcs["one"].aws_subnet.main["one"] will be created
  + resource "aws_subnet" "main" {
      + vpc_id                          = (known after apply)
      + assign_ipv6_address_on_creation = false
      + cidr_block                      = "10.1.0.0/24"
      + map_public_ip_on_launch         = false
    }

  # module.vpcs["one"].aws_vpc.default will be created
  + resource "aws_vpc" "default" {
      + id                               = (known after apply)
      + assign_generated_ipv6_cidr_block = false
      + cidr_block                       = "10.1.0.0/16"
      + enable_dns_support               = true
      + instance_tenancy                 = "default"
    }

  # module.vpcs["three"].aws_subnet.main["one"] will be created
  + resource "aws_subnet" "main" {
      + vpc_id                          = (known after apply)
      + assign_ipv6_address_on_creation = false
      + cidr_block                      = "10.3.0.0/24"
      + map_public_ip_on_launch         = false
    }

  # module.vpcs["three"].aws_vpc.default will be created
  + resource "aws_vpc" "default" {
      + id                               = (known after apply)
      + assign_generated_ipv6_cidr_block = false
      + cidr_block                       = "10.3.0.0/16"
      + enable_dns_support               = true
      + instance_tenancy                 = "default"
    }

  # module.vpcs["two"].aws_subnet.main["one"] will be created
  + resource "aws_subnet" "main" {
      + vpc_id                          = (known after apply)
      + assign_ipv6_address_on_creation = false
      + cidr_block                      = "10.2.0.0/24"
      + map_public_ip_on_launch         = false
    }

  # module.vpcs["two"].aws_vpc.default will be created
  + resource "aws_vpc" "default" {
      + id                               = (known after apply)
      + assign_generated_ipv6_cidr_block = false
      + cidr_block                       = "10.2.0.0/16"
      + enable_dns_support               = true
      + instance_tenancy                 = "default"
    }

Plan: 10 to add, 0 to change, 0 to destroy.

显然,您可以向数据结构添加其他属性以完成更多任务。 您可以使用键作为将标记到子网等的名称。这里有很多可能性。 享受。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM