From 84f6ec5232718416a3469ce337c371bd886bb410 Mon Sep 17 00:00:00 2001 From: Joey Davenport Date: Tue, 26 Apr 2022 16:21:05 -0600 Subject: [PATCH 1/2] Added ability for finer grained subnet configuration --- modules/vpc/README.md | 12 +++++++----- modules/vpc/main.tf | 4 ++-- modules/vpc/variables.tf | 18 ++++++++++++------ 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/modules/vpc/README.md b/modules/vpc/README.md index 7daa4cb..c09afd0 100644 --- a/modules/vpc/README.md +++ b/modules/vpc/README.md @@ -17,13 +17,13 @@ For this reason, we recommend managing the tags externally of the resource itsel | Name | Version | |------|---------| | [terraform](#requirement\_terraform) | >=1.0.0 | -| [aws](#requirement\_aws) | >= 3.45.0 | +| [aws](#requirement\_aws) | >= 3.64.2 | ## Providers | Name | Version | |------|---------| -| [aws](#provider\_aws) | >= 3.45.0 | +| [aws](#provider\_aws) | >= 3.64.2 | ## Modules @@ -52,11 +52,13 @@ No modules. | Name | Description | Type | Default | Required | |------|-------------|------|---------|:--------:| | [num\_azs](#input\_num\_azs) | The number of availability zones to provision | `number` | `2` | no | -| [private\_subnet\_start](#input\_private\_subnet\_start) | n/a | `number` | `10` | no | +| [private\_subnet\_start](#input\_private\_subnet\_start) | The starting octet for the private subnet CIDR blocks generated by this module. | `number` | `10` | no | | [public\_subnet\_auto\_ip](#input\_public\_subnet\_auto\_ip) | n/a | `bool` | `false` | no | -| [public\_subnet\_start](#input\_public\_subnet\_start) | n/a | `number` | `20` | no | +| [public\_subnet\_start](#input\_public\_subnet\_start) | The starting octet for the public subnet CIDR blocks generated by this module. | `number` | `20` | no | | [region](#input\_region) | n/a | `string` | n/a | yes | -| [vpc\_cidr](#input\_vpc\_cidr) | n/a | `any` | n/a | yes | +| [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 | +| [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 | +| [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 | | [vpc\_name](#input\_vpc\_name) | The name used for the VPC and associated resources | `string` | n/a | yes | ## Outputs diff --git a/modules/vpc/main.tf b/modules/vpc/main.tf index 52177ab..07d25bb 100644 --- a/modules/vpc/main.tf +++ b/modules/vpc/main.tf @@ -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) @@ -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) diff --git a/modules/vpc/variables.tf b/modules/vpc/variables.tf index 29e7838..88bb995 100644 --- a/modules/vpc/variables.tf +++ b/modules/vpc/variables.tf @@ -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" { + 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" { @@ -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 } From e0560c68d58db85862bd2b11116ab4a86bd41e93 Mon Sep 17 00:00:00 2001 From: Joey Davenport Date: Thu, 28 Apr 2022 14:10:52 -0600 Subject: [PATCH 2/2] Added newbits for public and private subnets --- modules/vpc/README.md | 3 ++- modules/vpc/main.tf | 4 ++-- modules/vpc/variables.tf | 20 +++++++++++++------- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/modules/vpc/README.md b/modules/vpc/README.md index c09afd0..51125d7 100644 --- a/modules/vpc/README.md +++ b/modules/vpc/README.md @@ -52,11 +52,12 @@ No modules. | Name | Description | Type | Default | Required | |------|-------------|------|---------|:--------:| | [num\_azs](#input\_num\_azs) | The number of availability zones to provision | `number` | `2` | no | +| [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 | | [private\_subnet\_start](#input\_private\_subnet\_start) | The starting octet for the private subnet CIDR blocks generated by this module. | `number` | `10` | no | | [public\_subnet\_auto\_ip](#input\_public\_subnet\_auto\_ip) | n/a | `bool` | `false` | no | +| [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 | | [public\_subnet\_start](#input\_public\_subnet\_start) | The starting octet for the public subnet CIDR blocks generated by this module. | `number` | `20` | no | | [region](#input\_region) | n/a | `string` | n/a | yes | -| [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 | | [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 | | [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 | | [vpc\_name](#input\_vpc\_name) | The name used for the VPC and associated resources | `string` | n/a | yes | diff --git a/modules/vpc/main.tf b/modules/vpc/main.tf index 07d25bb..3302ff7 100644 --- a/modules/vpc/main.tf +++ b/modules/vpc/main.tf @@ -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, var.subnet_newbits, 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) @@ -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, var.subnet_newbits, 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) diff --git a/modules/vpc/variables.tf b/modules/vpc/variables.tf index 88bb995..e7da9a9 100644 --- a/modules/vpc/variables.tf +++ b/modules/vpc/variables.tf @@ -37,21 +37,27 @@ variable "num_azs" { } variable "private_subnet_start" { - default = 10 + default = 10 description = "The starting octet for the private subnet CIDR blocks generated by this module." - type = number + type = number } variable "public_subnet_start" { - default = 20 + default = 20 description = "The starting octet for the public subnet CIDR blocks generated by this module." - type = number + type = number } -variable "subnet_newbits" { - default = 8 +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 + 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" {