From 697ce226d2fbd4e90065574ce2cf2b0f7216ae68 Mon Sep 17 00:00:00 2001 From: xpk Date: Thu, 7 Dec 2023 18:02:39 +0800 Subject: [PATCH] UPD: module now supports more than 2 AZs. Still defaults to 2 for compatibility --- modules/networking/vpc-subnet-manual/main.tf | 33 ++++++++++--------- .../networking/vpc-subnet-manual/variables.tf | 6 ++++ 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/modules/networking/vpc-subnet-manual/main.tf b/modules/networking/vpc-subnet-manual/main.tf index 1e7566e..5e08c13 100644 --- a/modules/networking/vpc-subnet-manual/main.tf +++ b/modules/networking/vpc-subnet-manual/main.tf @@ -1,9 +1,3 @@ -data "aws_caller_identity" "this" {} - -data "aws_availability_zones" "available-az" { - state = "available" -} - data "aws_default_tags" "this" { lifecycle { postcondition { @@ -13,29 +7,36 @@ data "aws_default_tags" "this" { } } +data "aws_caller_identity" "this" {} + +data "aws_availability_zones" "available-az" { + state = "available" +} + + locals { - no-az = 2 # hard-coding to 2AZ - vpc-cidr = var.vpc-cidr + vpc-cidr = var.vpc-cidr + enabled-az-names = slice(data.aws_availability_zones.available-az.names, 0, var.num_azs) } resource "aws_subnet" "private-subnets" { count = length(var.private-subnet-cidrs) vpc_id = aws_vpc.vpc.id - availability_zone = element(data.aws_availability_zones.available-az.names, count.index % 2) + availability_zone = element(local.enabled-az-names, count.index) cidr_block = var.private-subnet-cidrs[count.index] - tags = merge(data.aws_default_tags.this.tags, { - Name = "${var.resource-prefix}-private-${split("-", element(data.aws_availability_zones.available-az.names, count.index))[2]}-${count.index + 1}" - }) + tags = { + Name = "${var.resource-prefix}-private-${split("-", element(local.enabled-az-names, count.index))[2]}-${count.index + 1}" + } } resource "aws_subnet" "public-subnets" { count = length(var.public-subnet-cidrs) vpc_id = aws_vpc.vpc.id - availability_zone = element(data.aws_availability_zones.available-az.names, count.index % 2) + availability_zone = element(local.enabled-az-names, count.index) cidr_block = var.public-subnet-cidrs[count.index] - tags = merge(data.aws_default_tags.this.tags, { - Name = "${var.resource-prefix}-public-${split("-", element(data.aws_availability_zones.available-az.names, count.index))[2]}-${count.index + 1}" - }) + tags = { + Name = "${var.resource-prefix}-public-${split("-", element(local.enabled-az-names, count.index))[2]}-${count.index + 1}" + } } resource "aws_vpc" "vpc" { diff --git a/modules/networking/vpc-subnet-manual/variables.tf b/modules/networking/vpc-subnet-manual/variables.tf index c2f6b74..d9a3ceb 100644 --- a/modules/networking/vpc-subnet-manual/variables.tf +++ b/modules/networking/vpc-subnet-manual/variables.tf @@ -34,4 +34,10 @@ variable "secondary_cidr_blocks" { type = list(string) description = "Additional cidr blocks" default = [] +} + +variable num_azs { + description = "By default, use 2 AZs for redundancy" + type = number + default = 2 } \ No newline at end of file