Skip to content

Commit f438afe

Browse files
authored
Add single NAT Gateway flag. (#3)
* add single nat option * remove aws profile * add pre-commit * update README.md * dummy update * update README.md
1 parent 5a404f4 commit f438afe

5 files changed

Lines changed: 85 additions & 15 deletions

File tree

.pre-commit-config.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
repos:
2+
- repo: git://github.com/antonbabenko/pre-commit-terraform
3+
rev: v1.27.0
4+
hooks:
5+
- id: terraform_fmt
6+
- id: terraform_docs

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,42 @@ This Terraform module creates the base networking infrastructure on AWS.
1414
Check valid versions on:
1515
* Github Releases: <https://github.com/cn-terraform/terraform-aws-networking/releases>
1616
* Terraform Module Registry: <https://registry.terraform.io/modules/cn-terraform/networking/aws>
17+
18+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
19+
## Requirements
20+
21+
| Name | Version |
22+
| --------- | ------- |
23+
| terraform | >= 0.13 |
24+
25+
## Providers
26+
27+
| Name | Version |
28+
| ---- | ------- |
29+
| aws | n/a |
30+
31+
## Inputs
32+
33+
| Name | Description | Type | Default | Required |
34+
| ------------------------------------------------ | ------------------------------------------------------------------ | ------ | ------- | :------: |
35+
| availability\_zones | List of availability zones to be used by subnets | `list` | n/a | yes |
36+
| name\_prefix | Name prefix for resources on AWS | `any` | n/a | yes |
37+
| private\_subnets\_cidrs\_per\_availability\_zone | List of CIDRs to use on each availability zone for private subnets | `list` | n/a | yes |
38+
| public\_subnets\_cidrs\_per\_availability\_zone | List of CIDRs to use on each availability zone for public subnets | `list` | n/a | yes |
39+
| single\_nat | enable single NAT Gateway | `bool` | `false` | no |
40+
| vpc\_cidr\_block | AWS VPC CIDR Block | `any` | n/a | yes |
41+
42+
## Outputs
43+
44+
| Name | Description |
45+
| ---------------------------------- | ------------------------------------------------------------------------------------------------------ |
46+
| availability\_zones | List of availability zones used by subnets |
47+
| internet\_gateway\_id | ID of the generated Internet Gateway |
48+
| nat\_gw\_ids | List with the IDs of the NAT Gateways created on public subnets to provide internet to private subnets |
49+
| private\_subnets\_ids | List with the Private Subnets IDs |
50+
| private\_subnets\_route\_table\_id | ID of the Route Table used on Private networks |
51+
| public\_subnets\_ids | List with the Public Subnets IDs |
52+
| public\_subnets\_route\_table\_id | ID of the Route Tables used on Public networks |
53+
| vpc\_cidr\_block | The CIDR block of the VPC |
54+
| vpc\_id | The ID of the VPC |
55+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

examples/test/main.tf

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,22 @@ provider "aws" {
33
}
44

55
module "base-network" {
6-
source = "../../"
7-
name_prefix = "base"
8-
vpc_cidr_block = "192.168.0.0/16"
9-
availability_zones = ["us-east-1a", "us-east-1b", "us-east-1c", "us-east-1d"]
10-
public_subnets_cidrs_per_availability_zone = ["192.168.0.0/19", "192.168.32.0/19", "192.168.64.0/19", "192.168.96.0/19"]
11-
private_subnets_cidrs_per_availability_zone = ["192.168.128.0/19", "192.168.160.0/19", "192.168.192.0/19", "192.168.224.0/19"]
6+
source = "../../"
7+
name_prefix = "test"
8+
single_nat = true
9+
vpc_cidr_block = "192.168.0.0/16"
10+
11+
availability_zones = [
12+
"us-east-1a",
13+
"us-east-1b"
14+
]
15+
public_subnets_cidrs_per_availability_zone = [
16+
"192.168.0.0/19",
17+
"192.168.32.0/19"
18+
]
19+
private_subnets_cidrs_per_availability_zone = [
20+
"192.168.128.0/19",
21+
"192.168.160.0/19"
22+
]
23+
1224
}

main.tf

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
# AWS Virtual Private Network
33
#------------------------------------------------------------------------------
44
resource "aws_vpc" "vpc" {
5-
cidr_block = var.vpc_cidr_block # The CIDR block for the VPC.
6-
enable_dns_support = true # A boolean flag to enable/disable DNS support in the VPC.
7-
enable_dns_hostnames = true # A boolean flag to enable/disable DNS hostnames in the VPC.
5+
cidr_block = var.vpc_cidr_block
6+
# The CIDR block for the VPC.
7+
enable_dns_support = true
8+
# A boolean flag to enable/disable DNS support in the VPC.
9+
enable_dns_hostnames = true
10+
# A boolean flag to enable/disable DNS hostnames in the VPC.
811
tags = {
912
Name = "${var.name_prefix}-vpc"
1013
}
@@ -37,7 +40,7 @@ resource "aws_subnet" "public_subnets" {
3740

3841
# Elastic IPs for NAT
3942
resource "aws_eip" "nat_eip" {
40-
count = length(var.availability_zones)
43+
count = var.single_nat ? 1 : length(var.availability_zones)
4144
vpc = true
4245
tags = {
4346
Name = "${var.name_prefix}-nat-eip-${element(var.availability_zones, count.index)}"
@@ -46,13 +49,17 @@ resource "aws_eip" "nat_eip" {
4649

4750
# NAT Gateways
4851
resource "aws_nat_gateway" "nat_gw" {
49-
count = length(var.availability_zones)
50-
depends_on = [aws_internet_gateway.internet_gw]
51-
allocation_id = element(aws_eip.nat_eip.*.id, count.index)
52-
subnet_id = element(aws_subnet.public_subnets.*.id, count.index)
52+
count = var.single_nat ? 1 : length(var.availability_zones)
53+
allocation_id = var.single_nat ? aws_eip.nat_eip.0.id : element(aws_eip.nat_eip.*.id, count.index)
54+
subnet_id = var.single_nat ? aws_subnet.public_subnets.0.id : element(aws_subnet.public_subnets.*.id, count.index)
55+
5356
tags = {
5457
Name = "${var.name_prefix}-nat-gw-${element(var.availability_zones, count.index)}"
5558
}
59+
60+
depends_on = [
61+
aws_internet_gateway.internet_gw
62+
]
5663
}
5764

5865
# Public route table
@@ -116,7 +123,7 @@ resource "aws_route" "private_internet_route" {
116123
]
117124
route_table_id = element(aws_route_table.private_subnets_route_table.*.id, count.index)
118125
destination_cidr_block = "0.0.0.0/0"
119-
nat_gateway_id = element(aws_nat_gateway.nat_gw.*.id, count.index)
126+
nat_gateway_id = var.single_nat ? aws_nat_gateway.nat_gw.0.id : element(aws_nat_gateway.nat_gw.*.id, count.index)
120127
}
121128

122129
# Association of Route Table to Subnets

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,9 @@ variable "private_subnets_cidrs_per_availability_zone" {
2929
type = list
3030
description = "List of CIDRs to use on each availability zone for private subnets"
3131
}
32+
33+
variable "single_nat" {
34+
type = bool
35+
default = false
36+
description = "enable single NAT Gateway"
37+
}

0 commit comments

Comments
 (0)