From 95a62ca1d5dac9484c47d2286a2847401f4c36c1 Mon Sep 17 00:00:00 2001 From: xpk Date: Fri, 29 Jan 2021 11:21:22 +0800 Subject: [PATCH] FIX: previously, 2 subnets are created regardless of number of AZs --- vpc-subnets/vpc.tf | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/vpc-subnets/vpc.tf b/vpc-subnets/vpc.tf index bf693ab..61c1274 100644 --- a/vpc-subnets/vpc.tf +++ b/vpc-subnets/vpc.tf @@ -8,6 +8,34 @@ module "random" { source = "./m.random" } +module "public-subnets" { + source = "hashicorp/subnets/cidr" + + base_cidr_block = local.subnet_start[0] + + networks = [ + for az in data.aws_availability_zones.available.names : + { + name = az + new_bits = 4 + } + ] +} + +module "private-subnets" { + source = "hashicorp/subnets/cidr" + + base_cidr_block = local.subnet_start[1] + + networks = [ + for az in data.aws_availability_zones.available.names : + { + name = az + new_bits = 4 + } + ] +} + module "vpc01" { source = "terraform-aws-modules/vpc/aws" version = "2.47.0" @@ -15,8 +43,8 @@ module "vpc01" { name = "demo-vpc-${module.random.number}" cidr = var.vpc-cidr azs = data.aws_availability_zones.available.names - private_subnets = cidrsubnets(local.subnet_start[0], 4, 4) - public_subnets = cidrsubnets(local.subnet_start[1], 4, 4) + private_subnets = values(module.public-subnets.network_cidr_blocks) + public_subnets = values(module.private-subnets.network_cidr_blocks) enable_nat_gateway = false single_nat_gateway = true enable_dns_hostnames = true