Skip to content

Commit

Permalink
[module/vpc] Allow for finer grained VPC configuration (#65)
Browse files Browse the repository at this point in the history
* Added ability for finer grained subnet configuration

* Added newbits for public and private subnets
  • Loading branch information
jrsdav authored Apr 29, 2022
1 parent c3fd480 commit be5831b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
13 changes: 8 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,14 @@ 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_newbits"></a> [private\_subnet\_newbits](#input\_private\_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_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_newbits"></a> [public\_subnet\_newbits](#input\_public\_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_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_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.public_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.private_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
28 changes: 20 additions & 8 deletions modules/vpc/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,27 @@ variable "num_azs" {
}

variable "private_subnet_start" {
type = number
default = 10
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
default = 20
description = "The starting octet for the public subnet CIDR blocks generated by this module."
type = number
}

variable "private_subnet_newbits" {
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_newbits" {
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 +72,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
}

0 comments on commit be5831b

Please sign in to comment.