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

[module/vpc] Allow for finer grained VPC configuration #65

Merged
merged 2 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 7 additions & 5 deletions modules/vpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ For this reason, we recommend managing the tags externally of the resource itsel
| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >=1.0.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.45.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.64.2 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.45.0 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.64.2 |

## Modules

Expand Down Expand Up @@ -52,11 +52,13 @@ No modules.
| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_num_azs"></a> [num\_azs](#input\_num\_azs) | The number of availability zones to provision | `number` | `2` | no |
| <a name="input_private_subnet_start"></a> [private\_subnet\_start](#input\_private\_subnet\_start) | n/a | `number` | `10` | no |
| <a name="input_private_subnet_start"></a> [private\_subnet\_start](#input\_private\_subnet\_start) | The starting octet for the private subnet CIDR blocks generated by this module. | `number` | `10` | no |
| <a name="input_public_subnet_auto_ip"></a> [public\_subnet\_auto\_ip](#input\_public\_subnet\_auto\_ip) | n/a | `bool` | `false` | no |
| <a name="input_public_subnet_start"></a> [public\_subnet\_start](#input\_public\_subnet\_start) | n/a | `number` | `20` | no |
| <a name="input_public_subnet_start"></a> [public\_subnet\_start](#input\_public\_subnet\_start) | The starting octet for the public subnet CIDR blocks generated by this module. | `number` | `20` | no |
| <a name="input_region"></a> [region](#input\_region) | n/a | `string` | n/a | yes |
| <a name="input_vpc_cidr"></a> [vpc\_cidr](#input\_vpc\_cidr) | n/a | `any` | n/a | yes |
| <a name="input_subnet_newbits"></a> [subnet\_newbits](#input\_subnet\_newbits) | The number of bits to added to the VPC CIDR prefix. For instance, if your VPC CIDR is a /16 and you set this number to 8, the subnets will be /24s. | `number` | `8` | no |
| <a name="input_tags"></a> [tags](#input\_tags) | Additional to apply to the resources. Note that this module sets the tags Name, Type, and Vendor by default. They can be overwritten, but it is not recommended. | `map(string)` | `{}` | no |
| <a name="input_vpc_cidr"></a> [vpc\_cidr](#input\_vpc\_cidr) | The CIDR range to be used by the AWS VPC. We recommend using a /16 prefix to automatically generate /24 subnets. If you are using a smaller or larger prefix, refer to the subnet\_newbits variable to ensure that the generated subnet ranges are a valid for EKS (minimum /24 is recommended). | `string` | n/a | yes |
| <a name="input_vpc_name"></a> [vpc\_name](#input\_vpc\_name) | The name used for the VPC and associated resources | `string` | n/a | yes |

## Outputs
Expand Down
4 changes: 2 additions & 2 deletions modules/vpc/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ resource "aws_vpc" "vpc" {
resource "aws_subnet" "public" {
count = var.num_azs
vpc_id = aws_vpc.vpc.id
cidr_block = cidrsubnet(var.vpc_cidr, 8, var.public_subnet_start + count.index)
cidr_block = cidrsubnet(var.vpc_cidr, var.subnet_newbits, var.public_subnet_start + count.index)
availability_zone = data.aws_availability_zones.available.names[count.index]
map_public_ip_on_launch = var.public_subnet_auto_ip
tags = merge({ "Vendor" = "StreamNative", "Type" = "public", Name = format("%s-public-sbn-%s", var.vpc_name, count.index) }, var.tags)
Expand All @@ -48,7 +48,7 @@ resource "aws_subnet" "public" {
resource "aws_subnet" "private" {
count = var.num_azs
vpc_id = aws_vpc.vpc.id
cidr_block = cidrsubnet(var.vpc_cidr, 8, var.private_subnet_start + count.index)
cidr_block = cidrsubnet(var.vpc_cidr, var.subnet_newbits, var.private_subnet_start + count.index)
availability_zone = data.aws_availability_zones.available.names[count.index]
tags = merge({ "Vendor" = "StreamNative", "Type" = "private", Name = format("%s-private-sbn-%s", var.vpc_name, count.index) }, var.tags)

Expand Down
18 changes: 12 additions & 6 deletions modules/vpc/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,21 @@ variable "num_azs" {
}

variable "private_subnet_start" {
type = number
default = 10
description = "The starting octet for the private subnet CIDR blocks generated by this module."
type = number
}

variable "public_subnet_start" {
type = number
default = 20
description = "The starting octet for the public subnet CIDR blocks generated by this module."
type = number
}

variable "subnet_newbits" {
maxsxu marked this conversation as resolved.
Show resolved Hide resolved
default = 8
description = "The number of bits to added to the VPC CIDR prefix. For instance, if your VPC CIDR is a /16 and you set this number to 8, the subnets will be /24s."
type = number
}

variable "public_subnet_auto_ip" {
Expand All @@ -58,8 +66,6 @@ variable "tags" {
}

variable "vpc_cidr" {
validation {
condition = can(regex("^10\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}/16", var.vpc_cidr))
error_message = "The vpc_cidr must be a 10.x.x.x range with /16 CIDR."
}
description = "The CIDR range to be used by the AWS VPC. We recommend using a /16 prefix to automatically generate /24 subnets. If you are using a smaller or larger prefix, refer to the subnet_newbits variable to ensure that the generated subnet ranges are a valid for EKS (minimum /24 is recommended)."
type = string
}