Skip to content

Commit

Permalink
feat: add availability_zones to vpc module (#135)
Browse files Browse the repository at this point in the history
<!--
  ~ Copyright 2023 StreamNative, Inc.
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~     http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
-->

<!--
### Contribution Checklist

- Fill out the template below to describe the changes contributed by the
pull request. That will give reviewers the context they need to do the
review.
  
- Each pull request should address only one issue, not mix up code from
multiple issues.
  
  - Each commit in the pull request has a meaningful commit message

- Once all items of the checklist are addressed, remove the above text
and this checklist, leaving only the filled out template below.

**(The sections below can be removed for hotfixes of typos)**
-->

### Motivation

Allow to specify the desired AZs when provisioning VPC and Subnets. If
specified will ignore the `num_azs`

### Modifications

- Add variable `availability_zones`

### Verifying this change

- [x] Make sure that the change passes the CI checks.

*(Please pick either of the following options)*

This change is a trivial rework / code cleanup without any test
coverage.

*(or)*

This change is already covered by existing tests, such as *(please
describe tests)*.

*(or)*

This change added tests and can be verified as follows:

*(example:)*
- *Added integration tests for end-to-end deployment with large payloads
(10MB)*
  - *Extended integration test for recovery after broker failure*

### Documentation

- [x] `no-need-doc`
  • Loading branch information
maxsxu authored Jun 28, 2024
1 parent ce55ce2 commit c85f5e2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
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

0 comments on commit c85f5e2

Please sign in to comment.