Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add availability_zones to vpc module #135

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions modules/vpc/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ data "aws_availability_zones" "available" {
state = "available"
}

locals {
azs = length(var.availability_zones) > 0 ? var.availability_zones : data.aws_availability_zones.available.names
num_azs = length(var.availability_zones) > 0 ? length(var.availability_zones) : var.num_azs
}

resource "aws_vpc" "vpc" {
cidr_block = var.vpc_cidr
enable_dns_support = true
Expand All @@ -28,11 +33,11 @@ resource "aws_vpc" "vpc" {
}

resource "aws_subnet" "public" {
count = var.num_azs
count = local.num_azs

vpc_id = aws_vpc.vpc.id
cidr_block = cidrsubnet(var.vpc_cidr, var.public_subnet_newbits, var.public_subnet_start + count.index)
availability_zone = data.aws_availability_zones.available.names[count.index]
availability_zone = local.azs[count.index]
map_public_ip_on_launch = var.disable_nat_gateway ? true : var.public_subnet_auto_ip
tags = merge({ "Vendor" = "StreamNative", "Type" = "public", "kubernetes.io/role/elb" = "1", Name = format("%s-public-sbn-%s", var.vpc_name, count.index) }, var.tags)

Expand All @@ -42,11 +47,11 @@ resource "aws_subnet" "public" {
}

resource "aws_subnet" "private" {
count = var.num_azs
count = local.num_azs

vpc_id = aws_vpc.vpc.id
cidr_block = cidrsubnet(var.vpc_cidr, var.private_subnet_newbits, var.private_subnet_start + count.index)
availability_zone = data.aws_availability_zones.available.names[count.index]
availability_zone = local.azs[count.index]
tags = merge({ "Vendor" = "StreamNative", "Type" = "private", "kubernetes.io/role/internal-elb" = "1", Name = format("%s-private-sbn-%s", var.vpc_name, count.index) }, var.tags)

lifecycle {
Expand All @@ -64,7 +69,7 @@ resource "aws_internet_gateway" "gw" {
}

resource "aws_eip" "eip" {
count = var.disable_nat_gateway ? 0 : var.num_azs
count = var.disable_nat_gateway ? 0 : local.num_azs

domain = "vpc"
tags = merge({ "Vendor" = "StreamNative", Name = format("%s-eip-%s", var.vpc_name, count.index) }, var.tags)
Expand All @@ -76,7 +81,7 @@ resource "aws_eip" "eip" {
}

resource "aws_nat_gateway" "nat_gw" {
count = var.disable_nat_gateway ? 0 : var.num_azs
count = var.disable_nat_gateway ? 0 : local.num_azs

allocation_id = aws_eip.eip[count.index].id
subnet_id = aws_subnet.public[count.index].id
Expand Down Expand Up @@ -107,14 +112,14 @@ resource "aws_route" "public_route" {
}

resource "aws_route_table_association" "public_assoc" {
count = var.num_azs
count = local.num_azs

subnet_id = aws_subnet.public[count.index].id
route_table_id = aws_route_table.public_route_table[0].id
}

resource "aws_route_table" "private_route_table" {
count = var.disable_nat_gateway ? 0 : var.num_azs
count = var.disable_nat_gateway ? 0 : local.num_azs

vpc_id = aws_vpc.vpc.id
tags = merge({ "Vendor" = "StreamNative", Name = format("%s-private-rtb-%s", var.vpc_name, count.index) }, var.tags)
Expand All @@ -125,15 +130,15 @@ resource "aws_route_table" "private_route_table" {
}

resource "aws_route" "private_route" {
count = var.disable_nat_gateway ? 0 : var.num_azs
count = var.disable_nat_gateway ? 0 : local.num_azs

route_table_id = aws_route_table.private_route_table[count.index].id
nat_gateway_id = aws_nat_gateway.nat_gw[count.index].id
destination_cidr_block = "0.0.0.0/0"
}

resource "aws_route_table_association" "private_assoc" {
count = var.disable_nat_gateway ? 0 : var.num_azs
count = var.disable_nat_gateway ? 0 : local.num_azs

subnet_id = aws_subnet.private[count.index].id
route_table_id = aws_route_table.private_route_table[count.index].id
Expand Down
6 changes: 6 additions & 0 deletions modules/vpc/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ variable "num_azs" {
default = 2
}

variable "availability_zones" {
type = list(string)
description = "The availability zones to provision. If specified will ignore num_azs"
default = []
}

variable "private_subnet_start" {
default = 0
description = "The starting octet for the private subnet CIDR blocks generated by this module."
Expand Down
Loading