Skip to content

Commit 5485631

Browse files
Merge pull request #1 from devops-contribution/feat/convert-into-private
update
2 parents 540a942 + db659e3 commit 5485631

3 files changed

Lines changed: 100 additions & 25 deletions

File tree

modules/vpc/main.tf

Lines changed: 87 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ terraform {
44
required_providers {
55
aws = {
66
source = "hashicorp/aws"
7-
version = "~> 5.0" # Allows any version 5.x.x
7+
version = "~> 5.0"
88
}
99
}
1010
}
1111

12-
1312
resource "aws_vpc" "eks_vpc" {
1413
cidr_block = var.vpc_cidr
1514
instance_tenancy = "default"
@@ -20,44 +19,111 @@ resource "aws_vpc" "eks_vpc" {
2019
}
2120
}
2221

23-
resource "aws_internet_gateway" "eks_internet_gateway" {
24-
vpc_id = aws_vpc.eks_vpc.id
25-
}
26-
27-
# Using data source to get all Avalablility Zones in region
22+
# Data source to get all Availability Zones in the region
2823
data "aws_availability_zones" "available_zones" {}
2924

30-
resource "aws_subnet" "public_subnet_az1" {
25+
# Private Subnet in AZ1
26+
resource "aws_subnet" "private_subnet_az1" {
3127
vpc_id = aws_vpc.eks_vpc.id
32-
cidr_block = var.public_subnet_az1_cidr
28+
cidr_block = var.private_subnet_az1_cidr
3329
availability_zone = data.aws_availability_zones.available_zones.names[0]
34-
map_public_ip_on_launch = true
30+
map_public_ip_on_launch = false
31+
32+
tags = {
33+
Name = "${var.customer}-private-subnet-az1"
34+
}
3535
}
3636

37-
resource "aws_subnet" "public_subnet_az2" {
37+
# Private Subnet in AZ2
38+
resource "aws_subnet" "private_subnet_az2" {
3839
vpc_id = aws_vpc.eks_vpc.id
39-
cidr_block = var.public_subnet_az2_cidr
40+
cidr_block = var.private_subnet_az2_cidr
4041
availability_zone = data.aws_availability_zones.available_zones.names[1]
41-
map_public_ip_on_launch = true
42+
map_public_ip_on_launch = false
43+
44+
tags = {
45+
Name = "${var.customer}-private-subnet-az2"
46+
}
47+
}
48+
49+
# Public Subnet for NAT Gateway
50+
resource "aws_subnet" "public_subnet" {
51+
vpc_id = aws_vpc.eks_vpc.id
52+
cidr_block = var.public_subnet_nat_cidr
53+
availability_zone = data.aws_availability_zones.available_zones.names[0]
54+
map_public_ip_on_launch = true # Public for NAT
55+
56+
tags = {
57+
Name = "${var.customer}-public-subnet-for-nat"
58+
}
59+
}
60+
61+
# Internet Gateway (needed for NAT Gateway)
62+
resource "aws_internet_gateway" "eks_internet_gateway" {
63+
vpc_id = aws_vpc.eks_vpc.id
64+
65+
tags = {
66+
Name = "${var.customer}-eks-igw"
67+
}
4268
}
4369

70+
# Elastic IP for NAT Gateway
71+
resource "aws_eip" "nat_eip" {
72+
domain = "vpc"
73+
}
74+
75+
# NAT Gateway in Public Subnet
76+
resource "aws_nat_gateway" "nat_gw" {
77+
allocation_id = aws_eip.nat_eip.id
78+
subnet_id = aws_subnet.public_subnet.id
79+
80+
tags = {
81+
Name = "${var.customer}-nat-gateway"
82+
}
83+
}
84+
85+
# Private Route Table (Uses NAT Gateway)
86+
resource "aws_route_table" "private_route_table" {
87+
vpc_id = aws_vpc.eks_vpc.id
88+
89+
route {
90+
cidr_block = "0.0.0.0/0"
91+
nat_gateway_id = aws_nat_gateway.nat_gw.id
92+
}
93+
94+
tags = {
95+
Name = "${var.customer}-private-route-table"
96+
}
97+
}
98+
99+
# Associate Private Subnet in AZ1 with Private Route Table
100+
resource "aws_route_table_association" "private_subnet_az1_association" {
101+
subnet_id = aws_subnet.private_subnet_az1.id
102+
route_table_id = aws_route_table.private_route_table.id
103+
}
104+
105+
# Associate Private Subnet in AZ2 with Private Route Table
106+
resource "aws_route_table_association" "private_subnet_az2_association" {
107+
subnet_id = aws_subnet.private_subnet_az2.id
108+
route_table_id = aws_route_table.private_route_table.id
109+
}
110+
111+
# Public Route Table for NAT Gateway
44112
resource "aws_route_table" "public_route_table" {
45113
vpc_id = aws_vpc.eks_vpc.id
46114

47115
route {
48116
cidr_block = "0.0.0.0/0"
49117
gateway_id = aws_internet_gateway.eks_internet_gateway.id
50118
}
51-
}
52119

53-
# Associating Public Subnet in AZ1 to route table
54-
resource "aws_route_table_association" "public_subnet_az1_route_table_association" {
55-
subnet_id = aws_subnet.public_subnet_az1.id
56-
route_table_id = aws_route_table.public_route_table.id
120+
tags = {
121+
Name = "public-route-table"
122+
}
57123
}
58124

59-
# Associating Public Subnet in AZ2 to route table
60-
resource "aws_route_table_association" "public_subnet_az2_route_table_association" {
61-
subnet_id = aws_subnet.public_subnet_az2.id
125+
# Public Subnet with Public Route Table
126+
resource "aws_route_table_association" "public_subnet_association" {
127+
subnet_id = aws_subnet.public_subnet.id
62128
route_table_id = aws_route_table.public_route_table.id
63129
}

modules/vpc/outputs.tf

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@ output "vpc_id" {
22
value = aws_vpc.eks_vpc.id
33
}
44

5-
output "public_subnet_az1_id" {
5+
output "private_subnet_az1_id" {
66
value = aws_subnet.public_subnet_az1.id
77
}
88

9-
output "public_subnet_az2_id" {
9+
output "private_subnet_az2_id" {
1010
value = aws_subnet.public_subnet_az2.id
1111
}
1212

1313
output "internet_gateway" {
1414
value = aws_internet_gateway.eks_internet_gateway.id
1515
}
16+
17+
output "nat_gateway" {
18+
value =
19+
}

modules/vpc/variables.tf

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@ variable "vpc_cidr" {
33
#default = "10.0.0.0/16"
44
}
55

6-
variable "public_subnet_az1_cidr" {
6+
variable "private_subnet_az1_cidr" {
77
type = string
88
#default = "10.0.1.0/24"
99
}
1010

11-
variable "public_subnet_az2_cidr" {
11+
variable "private_subnet_az2_cidr" {
1212
type = string
1313
#default = "10.0.2.0/24"
1414
}
1515

16+
variable "public_subnet_nat_cidr" {
17+
type = string
18+
#default = "10.0.3.0/24"
19+
}
20+
1621
variable "customer" {
1722
type = string
1823
}

0 commit comments

Comments
 (0)