简体   繁体   English

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

[英]VPC and corresponding subnets creation in Terraform

I have to create 4 VPC, attaching 1 subnet to first 3 VPC;我必须创建 4 个 VPC,将 1 个子网附加到前 3 个 VPC; but last VPC will have 3 subnets.但最后一个 VPC 将有 3 个子网。

It's throwing error and the reason is when it's reaching to create subnet to 4th VPC it has still another two CIDR.. please help.它抛出错误,原因是当它到达为第四个 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)

} }

The new feature to allow for_each on modules is a really big deal for stuff like this.允许在模块上使用for_each的新功能对于这样的东西来说非常重要。 Make a module (I called it vpc).制作一个模块(我称之为 vpc)。 Then for_each over the module.然后在模块上 for_each。 Make a complex data structure that describes your network.制作一个复杂的数据结构来描述您的网络。 This requires terraform 0.13+这需要 terraform 0.13+

This is my directory contents.这是我的目录内容。 The folder is the vpc module I made.该文件夹是我制作的vpc模块。

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

1 directory, 3 files

This is the conent of the vpc module:这是 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
}

This is the contents of my root terraform code that calls the module.这是我调用模块的根 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
}

Here is the plan output:这是计划输出:

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.

Obviously, you can add other properties to the data structure to accomplish even more.显然,您可以向数据结构添加其他属性以完成更多任务。 You can use the keys as the names that will be labeled to the subnets, etc. Lots of possibilities here.您可以使用键作为将标记到子网等的名称。这里有很多可能性。 Enjoy.享受。

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

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