UPD: module now supports more than 2 AZs. Still defaults to 2 for compatibility

This commit is contained in:
xpk 2023-12-07 18:02:39 +08:00
parent 14aedda9a9
commit 697ce226d2
Signed by: xpk
GPG Key ID: CD4FF6793F09AB86
2 changed files with 23 additions and 16 deletions

View File

@ -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
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" {

View File

@ -35,3 +35,9 @@ variable "secondary_cidr_blocks" {
description = "Additional cidr blocks"
default = []
}
variable num_azs {
description = "By default, use 2 AZs for redundancy"
type = number
default = 2
}