简体   繁体   中英

VPC and corresponding subnets creation in Terraform

I have to create 4 VPC, attaching 1 subnet to first 3 VPC; but last VPC will have 3 subnets.

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.

    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. Make a module (I called it vpc). Then for_each over the module. Make a complex data structure that describes your network. This requires terraform 0.13+

This is my directory contents. The folder is the vpc module I made.

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

1 directory, 3 files

This is the conent of the vpc module:

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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