Skip to content

Commit 4ea7b11

Browse files
Added Subnet And NAT Gateway Modules
1 parent fe82af0 commit 4ea7b11

File tree

14 files changed

+293
-6
lines changed

14 files changed

+293
-6
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
# terraform-aws-vpc
2-
Hashicorp Terraform AWS VPC Module
1+
# AWS VPC Module
2+
Terraform AWS VPC Module

main.tf

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ terraform {
55

66
# AWS VPC Resources
77
resource "aws_vpc" "vpc" {
8-
cidr_block = "${var.cidr}"
8+
cidr_block = var.cidr
99

1010
tags = {
1111
Name = "${var.prefix}-${var.environment}"
@@ -18,7 +18,39 @@ resource "aws_internet_gateway" "igw" {
1818
vpc_id = aws_vpc.vpc.id
1919

2020
tags = {
21-
Name = "${var.prefix}-${var.environment}"
21+
Name = "${var.prefix}-${var.environment}"
2222
Environment = var.environment
2323
}
24-
}
24+
}
25+
26+
# AWS VPC Subnets Module - Public Subnet
27+
module "public_subnet" {
28+
source = "./modules/subnets"
29+
vpc_id = aws_vpc.vpc.id
30+
aws_internet_gateway_id = aws_internet_gateway.igw.id
31+
subnet_bits = var.subnet_bits
32+
cidr = var.cidr
33+
prefix = var.prefix
34+
environment = var.environment
35+
subnet_type = ["public"]
36+
}
37+
38+
# AWS NAT Gateway Module
39+
module "nat_gateway" {
40+
source = "./modules/nat-gateways"
41+
prefix = var.prefix
42+
environment = var.environment
43+
public_subnet_ids = module.public_subnet.public_subnet_ids
44+
}
45+
46+
# AWS VPC Subnets Module - Private Subnet
47+
# module "private_subnet" {
48+
# source = "./modules/subnets"
49+
# vpc_id = aws_vpc.vpc.id
50+
# aws_nat_gateway_id = module.nat_gateway.nat_gateway_ids
51+
# cidr = var.cidr
52+
# prefix = var.prefix
53+
# environment = var.environment
54+
# subnet_bits = var.subnet_bits
55+
# subnet_type = ["private", "storage"]
56+
# }

modules/nat-gateways/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# NAT Module

modules/nat-gateways/main.tf

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# AWS Availability Zones
2+
data "aws_availability_zones" "available_zones" {}
3+
4+
# AWS Elastic IPs
5+
resource "aws_eip" "eip" {
6+
vpc = true
7+
count = length(data.aws_availability_zones.available_zones.names)
8+
9+
tags = {
10+
Name = "${var.prefix}-${count.index + 1}"
11+
Environment = var.environment
12+
}
13+
}
14+
15+
# AWS NAT Gateway Binding - Public Subnets
16+
resource "aws_nat_gateway" "nat_gateway" {
17+
allocation_id = element(aws_eip.eip.*.id, count.index)
18+
subnet_id = element(var.public_subnet_ids, count.index)
19+
count = length(data.aws_availability_zones.available_zones.names)
20+
21+
tags = {
22+
Name = "${var.prefix}-${count.index + 1}"
23+
Environment = var.environment
24+
}
25+
}

modules/nat-gateways/output.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
output "nat_gateway_ids" {
2+
value = aws_nat_gateway.nat_gateway.*.id
3+
}
4+
5+
output "eip_ids" {
6+
value = aws_eip.eip.*.id
7+
}

modules/nat-gateways/variables.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
variable "prefix" {
2+
description = "generic naming resources"
3+
type = string
4+
}
5+
6+
variable "environment" {
7+
description = "To apply generic environment to AWS VPC Resources"
8+
type = string
9+
}
10+
11+
variable "public_subnet_ids" {
12+
description = "list of public subnets in order of availability zones so that NAT Gateway's can be created in those respective subnets"
13+
type = list
14+
}

modules/subnets/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Subnet Module

modules/subnets/main.tf

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# AWS Availability Zones
2+
data "aws_availability_zones" "available_zones" {}
3+
4+
# AWS Public Subnets
5+
module "aws_public_subnet" {
6+
source = "./resources"
7+
create = contains(var.subnet_type, "public") ? 1 : 0
8+
prefix = var.prefix
9+
environment = var.environment
10+
vpc_id = var.vpc_id
11+
cidr = var.cidr
12+
subnet_bits = var.subnet_bits
13+
subnet_type = "public"
14+
}
15+
16+
# AWS Private Subnets
17+
module "aws_private_subnet" {
18+
source = "./resources"
19+
create = contains(var.subnet_type, "private") ? 1 : 0
20+
prefix = var.prefix
21+
environment = var.environment
22+
vpc_id = var.vpc_id
23+
cidr = var.cidr
24+
offset = length(data.aws_availability_zones.available_zones.names)
25+
subnet_bits = var.subnet_bits
26+
subnet_type = "private"
27+
}
28+
29+
# AWS Storage Subnets
30+
module "aws_storage_subnet" {
31+
source = "./resources"
32+
create = contains(var.subnet_type, "storage") ? 1 : 0
33+
prefix = var.prefix
34+
environment = var.environment
35+
cidr = var.cidr
36+
vpc_id = var.vpc_id
37+
offset = 2 * length(data.aws_availability_zones.available_zones.names)
38+
subnet_bits = var.subnet_bits
39+
subnet_type = "storage"
40+
}
41+
42+
# AWS Route Tables - Public Route
43+
resource "aws_route" "public_route" {
44+
count = contains(var.subnet_type, "public") ? length(data.aws_availability_zones.available_zones.names) : 0
45+
route_table_id = module.aws_public_subnet.route_table_ids[count.index]
46+
destination_cidr_block = "0.0.0.0/0"
47+
gateway_id = var.aws_internet_gateway_id
48+
}
49+
50+
# AWS Route Tables - Private Route
51+
resource "aws_route" "private_route" {
52+
count = contains(var.subnet_type, "private") ? length(data.aws_availability_zones.available_zones.names) : 0
53+
route_table_id = module.aws_private_subnet.route_table_ids[count.index]
54+
destination_cidr_block = "0.0.0.0/0"
55+
nat_gateway_id = var.aws_nat_gateway_id[count.index]
56+
}

modules/subnets/output.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
output "public_subnet_ids" {
2+
value = module.aws_public_subnet.subnet_ids
3+
}
4+
5+
output "public_route_table_ids" {
6+
value = module.aws_public_subnet.route_table_ids
7+
}
8+
9+
output "private_subnet_ids" {
10+
value = module.aws_private_subnet.subnet_ids
11+
}
12+
13+
output "private_route_table_ids" {
14+
value = module.aws_private_subnet.route_table_ids
15+
}
16+
17+
output "storage_subnet_ids" {
18+
value = module.aws_storage_subnet.subnet_ids
19+
}
20+
21+
output "storage_route_table_ids" {
22+
value = module.aws_storage_subnet.route_table_ids
23+
}

modules/subnets/resources/main.tf

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# AWS Availability Zones
2+
data "aws_availability_zones" "available_zones" {}
3+
4+
# AWS Subnets
5+
resource "aws_subnet" "subnets" {
6+
vpc_id = var.vpc_id
7+
count = var.create > 0 ? length(data.aws_availability_zones.available_zones.names) : 0
8+
cidr_block = cidrsubnet(var.cidr, var.subnet_bits, var.offset + count.index)
9+
availability_zone = data.aws_availability_zones.available_zones.names[count.index]
10+
11+
tags = {
12+
Name = "${var.prefix}-${var.subnet_type}-${count.index + 1}"
13+
Environment = var.environment
14+
Type = var.subnet_type
15+
}
16+
}
17+
18+
# AWS Route Tables
19+
resource "aws_route_table" "route_table" {
20+
vpc_id = var.vpc_id
21+
count = var.create > 0 ? length(data.aws_availability_zones.available_zones.names) : 0
22+
23+
tags = {
24+
Name = "${var.prefix}-${var.subnet_type}-${count.index + 1}"
25+
Environment = var.environment
26+
Type = var.subnet_type
27+
}
28+
}
29+
30+
# AWS Route Table - Subnet Association
31+
resource "aws_route_table_association" "subnet_association" {
32+
count = var.create > 0 ? length(data.aws_availability_zones.available_zones.names) : 0
33+
subnet_id = element(aws_subnet.subnets.*.id, count.index)
34+
route_table_id = element(aws_route_table.route_table.*.id, count.index)
35+
}

0 commit comments

Comments
 (0)