Skip to content

Commit e782a89

Browse files
committed
first public release
0 parents  commit e782a89

File tree

10 files changed

+222
-0
lines changed

10 files changed

+222
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Robert Hafner
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Teraform AWS NAT Module
2+
3+
This module enables NAT in a specific VPC. It supports either a NAT Gateway or NAT Instance depending on preference, and can be launched in one or more Availability Zones.
4+
5+
This module should normally be called directly from the VPC module, but exists as a standalone module mostly for cleaner code.
6+
7+
## Usage
8+
9+
### Example
10+
11+
```terraform
12+
module "nat" {
13+
source = "tedivm/nat/aws"
14+
version = "~> 1.0"
15+
16+
vpc = aws_vpc.main
17+
max_availability_zones = var.max_availability_zones
18+
route_tables = aws_route_table.private
19+
subnets = aws_subnet.public
20+
high_availability = var.high_availability_nat
21+
tags = local.common_tags
22+
}
23+
```
24+
25+
### Outputs
26+
27+
* aws_instances
28+
* aws_security_group
29+
* aws_iam_role
30+
* aws_eip
31+
32+
## High Availability Mode
33+
34+
In High Availability Mode this module runs one NAT in each availability zone. This ensures that issues in one zone do not affect the services in others. High Availability Mode also assigns each NAT Instance an Elastic IP address speed up the transfer of network connections to replacement instances.
35+
36+
37+
## Resources Affected
38+
39+
* EC2 Instance
40+
* EIP (if high availability mode is set)
41+
* IAM Role (through the ec2_iam_profile module)
42+
* Routes
43+
* Security Group

eip.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
resource "aws_eip" "main" {
3+
count = var.high_availability ? local.nat_count : 0
4+
instance = aws_instance.main[count.index].id
5+
vpc = true
6+
tags = local.common_tags
7+
}

iam.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
module "instance_profile" {
3+
source = "../ec2_iam_profile"
4+
name = "${var.vpc.id}_NAT_Instance_Profile"
5+
tags = local.common_tags
6+
}

instance.tf

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
locals {
3+
nat_count = var.high_availability ? var.max_availability_zones : 1
4+
}
5+
6+
data "aws_ami" "ubuntu" {
7+
most_recent = true
8+
9+
filter {
10+
name = "name"
11+
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
12+
}
13+
14+
filter {
15+
name = "virtualization-type"
16+
values = ["hvm"]
17+
}
18+
19+
owners = ["099720109477"] # Canonical
20+
}
21+
22+
resource "aws_instance" "main" {
23+
count = local.nat_count
24+
ami = data.aws_ami.ubuntu.id
25+
instance_type = var.instance_type
26+
27+
monitoring = true
28+
source_dest_check = false
29+
subnet_id = var.subnets[count.index].id
30+
31+
iam_instance_profile = module.instance_profile.aws_iam_instance_profile.name
32+
vpc_security_group_ids = [aws_security_group.nat_traffic.id]
33+
34+
user_data = templatefile("${path.module}/templates/userdata.tmpl", { "VPC_CIDR" : var.vpc.cidr_block })
35+
36+
tags = local.common_tags
37+
}

outputs.tf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
output "aws_instances" {
3+
value = aws_instance.main
4+
}
5+
6+
output "aws_security_group" {
7+
value = aws_security_group.nat_traffic
8+
}
9+
10+
output "aws_iam_role" {
11+
value = module.instance_profile.aws_iam_role
12+
}
13+
14+
output "aws_eip" {
15+
value = aws_eip.main
16+
}

routes.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
resource "aws_route" "route_to_nat" {
3+
count = var.max_availability_zones
4+
route_table_id = var.route_tables[count.index].id
5+
destination_cidr_block = "0.0.0.0/0"
6+
instance_id = var.high_availability ? aws_instance.main[count.index].id : aws_instance.main[0].id
7+
}

sg.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
resource "aws_security_group" "nat_traffic" {
3+
name = "${var.vpc.id} NAT Traffic"
4+
description = "Allow internal VPC traffic and all outbound traffic"
5+
vpc_id = var.vpc.id
6+
7+
ingress {
8+
description = "All traffic from inside VPC"
9+
from_port = 0
10+
to_port = 0
11+
protocol = "-1"
12+
cidr_blocks = [var.vpc.cidr_block]
13+
}
14+
15+
egress {
16+
from_port = 0
17+
to_port = 0
18+
protocol = "-1"
19+
cidr_blocks = ["0.0.0.0/0"]
20+
}
21+
22+
tags = local.common_tags
23+
}

templates/userdata.tmpl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Content-Type: multipart/mixed; boundary="//"
2+
MIME-Version: 1.0
3+
4+
--//
5+
Content-Type: text/cloud-config; charset="us-ascii"
6+
MIME-Version: 1.0
7+
Content-Transfer-Encoding: 7bit
8+
Content-Disposition: attachment; filename="cloud-config.txt"
9+
10+
#cloud-config
11+
cloud_final_modules:
12+
- [scripts-user, always]
13+
14+
--//
15+
Content-Type: text/x-shellscript; charset="us-ascii"
16+
MIME-Version: 1.0
17+
Content-Transfer-Encoding: 7bit
18+
Content-Disposition: attachment; filename="userdata.txt"
19+
20+
#!/bin/bash
21+
22+
PRIMARY_INTERFACE=$(ip link | awk -F: '$0 !~ "lo|vir|wl|^[^0-9]"{print $2;getline}')
23+
24+
echo 1 > /proc/sys/net/ipv4/ip_forward
25+
iptables -t nat -A POSTROUTING -o $PRIMARY_INTERFACE -s ${VPC_CIDR} -j MASQUERADE
26+
27+
--//--

variables.tf

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
variable "vpc" {
3+
description = "The AWS_VPC that the NAT instance will be in."
4+
}
5+
6+
variable "subnets" {
7+
description = "A list of AWS Subnets from the Public Networks that NAT Instances can be launched into."
8+
}
9+
10+
variable "instance_type" {
11+
default = "t3.micro"
12+
}
13+
14+
variable "route_tables" {
15+
description = "A list of AWS Route Tables from the Private Networks that should route to NAT."
16+
}
17+
18+
variable "max_availability_zones" {
19+
description = "The number of availablity zones to launch in. This should match the route_tables and subnets count."
20+
}
21+
22+
variable "high_availability" {
23+
description = "When enabled servers will be launched in each subnet."
24+
type = bool
25+
default = false
26+
}
27+
28+
variable "tags" {
29+
description = "Tags to apply to all resources."
30+
default = {}
31+
}
32+
33+
locals {
34+
common_tags = merge(var.tags, { "Name" : try("${var.tags["Name"]}-NAT", "${var.vpc.id}-NAT") })
35+
}

0 commit comments

Comments
 (0)