Skip to content

Commit 6a6d5b1

Browse files
authored
Merge pull request #60 from DNXLabs/feature/per-tier-subnet-sizing
feat: add per-tier subnet sizing support
2 parents 3cb319c + 6bcd4d2 commit 6a6d5b1

File tree

7 files changed

+67
-7
lines changed

7 files changed

+67
-7
lines changed

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,24 @@ module "network" {
4343
}
4444
```
4545

46+
### Per-Tier Subnet Sizing (Optional)
47+
48+
For mixed subnet sizes, override `newbits` per tier:
49+
50+
```hcl
51+
module "network" {
52+
source = "git::https://github.com/DNXLabs/terraform-aws-network.git"
53+
54+
vpc_cidr = "10.39.32.0/21"
55+
newbits = 5 # Default /26
56+
57+
# Override per tier
58+
public_newbits = 5 # /26 (62 IPs)
59+
private_newbits = 3 # /24 (254 IPs)
60+
secure_newbits = 5 # /26 (62 IPs)
61+
}
62+
```
63+
4664
<!--- BEGIN_TF_DOCS --->
4765

4866
## Requirements
@@ -75,6 +93,7 @@ module "network" {
7593
| firewall\_custom\_rules | The stateful rule group rules specifications in Suricata file format, with one rule per line | `list(string)` | `[]` | no |
7694
| firewall\_domain\_list | List the domain names you want to take action on. | `list(any)` | <pre>[<br> ".amazonaws.com",<br> ".github.com"<br>]</pre> | no |
7795
| firewall\_netnum\_offset | Start with this subnet for secure ones, plus number of AZs | `number` | `14` | no |
96+
| firewall\_newbits | Number of bits to add to the vpc cidr for firewall subnets (overrides 'newbits' if set) | `number` | `null` | no |
7897
| kms\_key\_arn | The ARN of the KMS Key to use when encrypting log data. | `string` | `""` | no |
7998
| kubernetes\_clusters | List of kubernetes cluster names to creates tags in public and private subnets of this VPC | `list(string)` | `[]` | no |
8099
| kubernetes\_clusters\_secure | List of kubernetes cluster names to creates tags in secure subnets of this VPC | `list(string)` | `[]` | no |
@@ -86,23 +105,27 @@ module "network" {
86105
| name\_suffix | Adds a name suffix to all resources created | `string` | `""` | no |
87106
| nat | Deploy NAT instance(s) | `bool` | `true` | no |
88107
| network\_firewall | Enable or disable VPC Network Firewall | `bool` | `false` | no |
89-
| newbits | Number of bits to add to the vpc cidr when building subnets | `number` | `5` | no |
108+
| newbits | Number of bits to add to the vpc cidr when building subnets (applies to all tiers unless tier-specific values are set) | `number` | `5` | no |
90109
| private\_nacl\_allow\_cidrs | CIDRs to allow traffic from private subnet | `list(string)` | `[]` | no |
91110
| private\_netnum\_offset | Start with this subnet for private ones, plus number of AZs | `number` | `5` | no |
111+
| private\_newbits | Number of bits to add to the vpc cidr for private subnets (overrides 'newbits' if set) | `number` | `null` | no |
92112
| public\_nacl\_allow\_cidrs | CIDRs to allow traffic from public subnet | `list(string)` | `[]` | no |
93113
| public\_nacl\_icmp | Allows ICMP traffic to and from the public subnet | `bool` | `true` | no |
94114
| public\_nacl\_inbound\_tcp\_ports | TCP Ports to allow inbound on public subnet via NACLs (this list cannot be empty) | `list(string)` | <pre>[<br> "80",<br> "443",<br> "22",<br> "1194"<br>]</pre> | no |
95115
| public\_nacl\_inbound\_udp\_ports | UDP Ports to allow inbound on public subnet via NACLs (this list cannot be empty) | `list(string)` | `[]` | no |
96116
| public\_nacl\_outbound\_tcp\_ports | TCP Ports to allow outbound to external services (use [0] to allow all ports) | `list(string)` | <pre>[<br> "0"<br>]</pre> | no |
97117
| public\_nacl\_outbound\_udp\_ports | UDP Ports to allow outbound to external services (use [0] to allow all ports) | `list(string)` | <pre>[<br> "0"<br>]</pre> | no |
98118
| public\_netnum\_offset | Start with this subnet for public ones, plus number of AZs | `number` | `0` | no |
119+
| public\_newbits | Number of bits to add to the vpc cidr for public subnets (overrides 'newbits' if set) | `number` | `null` | no |
99120
| secure\_nacl\_allow\_cidrs | CIDRs to allow traffic from secure subnet | `list(string)` | `[]` | no |
100121
| secure\_nacl\_allow\_public | Allow traffic between public and secure | `bool` | `false` | no |
101122
| secure\_netnum\_offset | Start with this subnet for secure ones, plus number of AZs | `number` | `10` | no |
123+
| secure\_newbits | Number of bits to add to the vpc cidr for secure subnets (overrides 'newbits' if set) | `number` | `null` | no |
102124
| tags | Extra tags to attach to resources | `map(string)` | `{}` | no |
103125
| transit\_nacl\_inbound\_tcp\_ports | TCP Ports to allow inbound on transit subnet via NACLs (this list cannot be empty) | `list(string)` | <pre>[<br> "1194"<br>]</pre> | no |
104126
| transit\_nacl\_inbound\_udp\_ports | UDP Ports to allow inbound on transit subnet via NACLs (this list cannot be empty) | `list(string)` | <pre>[<br> "1194"<br>]</pre> | no |
105127
| transit\_netnum\_offset | Start with this subnet for secure ones, plus number of AZs | `number` | `15` | no |
128+
| transit\_newbits | Number of bits to add to the vpc cidr for transit subnets (overrides 'newbits' if set) | `number` | `null` | no |
106129
| transit\_subnet | Create a transit subnet for VPC peering (only central account) | `bool` | `false` | no |
107130
| vpc\_cidr | Network CIDR for the VPC | `string` | n/a | yes |
108131
| vpc\_cidr\_summ | Define cidr used to summarize subnets by tier | `string` | `"/0"` | no |

_variables.tf

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,37 @@ variable "multi_nat" {
3939
variable "newbits" {
4040
type = number
4141
default = 5
42-
description = "Number of bits to add to the vpc cidr when building subnets"
42+
description = "Number of bits to add to the vpc cidr when building subnets (applies to all tiers unless tier-specific values are set)"
43+
}
44+
45+
variable "public_newbits" {
46+
type = number
47+
default = null
48+
description = "Number of bits to add to the vpc cidr for public subnets (overrides 'newbits' if set)"
49+
}
50+
51+
variable "private_newbits" {
52+
type = number
53+
default = null
54+
description = "Number of bits to add to the vpc cidr for private subnets (overrides 'newbits' if set)"
55+
}
56+
57+
variable "secure_newbits" {
58+
type = number
59+
default = null
60+
description = "Number of bits to add to the vpc cidr for secure subnets (overrides 'newbits' if set)"
61+
}
62+
63+
variable "transit_newbits" {
64+
type = number
65+
default = null
66+
description = "Number of bits to add to the vpc cidr for transit subnets (overrides 'newbits' if set)"
67+
}
68+
69+
variable "firewall_newbits" {
70+
type = number
71+
default = null
72+
description = "Number of bits to add to the vpc cidr for firewall subnets (overrides 'newbits' if set)"
4373
}
4474

4575
variable "vpc_cidr_summ" {
@@ -301,6 +331,13 @@ variable "db_subnet_group_secure_name_compat" {
301331
}
302332

303333
locals {
334+
# Compute effective newbits per tier (tier-specific overrides global)
335+
public_newbits_effective = coalesce(var.public_newbits, var.newbits)
336+
private_newbits_effective = coalesce(var.private_newbits, var.newbits)
337+
secure_newbits_effective = coalesce(var.secure_newbits, var.newbits)
338+
transit_newbits_effective = coalesce(var.transit_newbits, var.newbits)
339+
firewall_newbits_effective = coalesce(var.firewall_newbits, var.newbits)
340+
304341
kubernetes_clusters = zipmap(
305342
formatlist("kubernetes.io/cluster/%s", var.kubernetes_clusters),
306343
[for cluster in var.kubernetes_clusters : var.kubernetes_clusters_type]

subnet-firewall.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ resource "aws_subnet" "firewall" {
44

55
cidr_block = cidrsubnet(
66
aws_vpc.default.cidr_block,
7-
var.newbits,
7+
local.firewall_newbits_effective,
88
count.index + var.firewall_netnum_offset,
99
)
1010

subnet-private.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ resource "aws_subnet" "private" {
44

55
cidr_block = cidrsubnet(
66
aws_vpc.default.cidr_block,
7-
var.newbits,
7+
local.private_newbits_effective,
88
count.index + var.private_netnum_offset,
99
)
1010

subnet-public.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ resource "aws_subnet" "public" {
33
vpc_id = aws_vpc.default.id
44
cidr_block = cidrsubnet(
55
aws_vpc.default.cidr_block,
6-
var.newbits,
6+
local.public_newbits_effective,
77
count.index + var.public_netnum_offset,
88
)
99
availability_zone = data.aws_availability_zones.available.names[count.index]

subnet-secure.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ resource "aws_subnet" "secure" {
33
vpc_id = aws_vpc.default.id
44
cidr_block = cidrsubnet(
55
aws_vpc.default.cidr_block,
6-
var.newbits,
6+
local.secure_newbits_effective,
77
count.index + var.secure_netnum_offset,
88
)
99
availability_zone = data.aws_availability_zones.available.names[count.index]

subnet-transit.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ resource "aws_subnet" "transit" {
33
vpc_id = aws_vpc.default.id
44
cidr_block = cidrsubnet(
55
aws_vpc.default.cidr_block,
6-
var.newbits,
6+
local.transit_newbits_effective,
77
count.index + var.transit_netnum_offset,
88
)
99
availability_zone = data.aws_availability_zones.available.names[count.index]

0 commit comments

Comments
 (0)