Skip to content

Commit 981c480

Browse files
committed
added NAT gateway, private_subnet, route tables
1 parent 797d86f commit 981c480

File tree

2 files changed

+95
-8
lines changed

2 files changed

+95
-8
lines changed

Full Deployment/variables.tf

+31-7
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ variable "public_subnet_cidrs" {
4646
}
4747
}
4848

49+
variable "private_subnet_cidrs" {
50+
description = "CIDR blocks for private subnets for each environment"
51+
type = map(list(string))
52+
default = {
53+
dev = ["10.0.10.0/24", "10.0.11.0/24"]
54+
staging = ["10.1.10.0/24", "10.1.11.0/24"]
55+
prod = ["10.2.10.0/24", "10.2.11.0/24"]
56+
}
57+
}
58+
59+
4960
variable "ingress_rules" {
5061
description = "List of ingress rules for the security group"
5162
type = list(object({
@@ -57,11 +68,25 @@ variable "ingress_rules" {
5768
}))
5869
default = [
5970
{
60-
description = "SSH access from office"
71+
description = "SSH access from specific IP"
6172
from_port = 22
6273
to_port = 22
6374
protocol = "tcp"
64-
cidr_blocks = ["your_office_ip/32"] # Replace with your actual office IP
75+
cidr_blocks = ["198.51.100.1/32"] # Replace with actual SSH accessible IP
76+
},
77+
{
78+
description = "HTTP access"
79+
from_port = 80
80+
to_port = 80
81+
protocol = "tcp"
82+
cidr_blocks = ["0.0.0.0/0"] # Open to the world, adjust as necessary
83+
},
84+
{
85+
description = "HTTPS access"
86+
from_port = 443
87+
to_port = 443
88+
protocol = "tcp"
89+
cidr_blocks = ["0.0.0.0/0"] # Open to the world, adjust as necessary
6590
}
6691
]
6792
}
@@ -77,12 +102,11 @@ variable "egress_rules" {
77102
}))
78103
default = [
79104
{
80-
description = "HTTPS access to the Internet"
81-
from_port = 443
82-
to_port = 443
83-
protocol = "tcp"
105+
description = "Allow all outbound traffic"
106+
from_port = 0
107+
to_port = 0
108+
protocol = "-1"
84109
cidr_blocks = ["0.0.0.0/0"]
85110
}
86111
]
87112
}
88-

Full Deployment/vpc.tf

+64-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,22 @@ resource "aws_subnet" "public_subnet" {
2121
}
2222

2323

24+
resource "aws_subnet" "private_subnet" {
25+
count = length(var.private_subnet_cidrs[var.environment])
26+
vpc_id = aws_vpc.ci_cd_demo_vpc.id
27+
cidr_block = var.private_subnet_cidrs[var.environment][count.index]
28+
map_public_ip_on_launch = false
29+
30+
tags = {
31+
Name = "Private-Subnet-${count.index + 1}-${var.environment}"
32+
Terraform = "true"
33+
Environment = var.environment
34+
}
35+
}
36+
37+
38+
39+
2440
resource "aws_internet_gateway" "ci_cd_demo_igw" {
2541
vpc_id = aws_vpc.ci_cd_demo_vpc.id
2642

@@ -46,4 +62,51 @@ resource "aws_route_table_association" "public_route_table_association" {
4662
count = length(aws_subnet.public_subnet)
4763
subnet_id = aws_subnet.public_subnet[count.index].id
4864
route_table_id = aws_route_table.public_route_table.id
49-
}
65+
}
66+
67+
resource "aws_eip" "nat" {
68+
vpc = true
69+
}
70+
71+
#Public Subnet for NAT Gateway:
72+
#Ensure you have a public subnet that can host the NAT Gateway.
73+
74+
#Create the NAT Gateway:
75+
#This example assumes you have an Elastic IP (EIP) allocated for the NAT Gateway.
76+
77+
78+
resource "aws_nat_gateway" "nat_gateway" {
79+
allocation_id = aws_eip.nat.id
80+
subnet_id = aws_subnet.public_subnet[0].id
81+
82+
tags = {
83+
Name = "NAT-Gateway-${var.environment}"
84+
Terraform = "true"
85+
Environment = var.environment
86+
}
87+
}
88+
89+
#Configure Route Tables for Private Subnets:
90+
#Route tables need to direct traffic from private subnets to the NAT gateway for internet access
91+
92+
resource "aws_route_table" "private_route_table" {
93+
vpc_id = aws_vpc.ci_cd_demo_vpc.id
94+
95+
route {
96+
cidr_block = "0.0.0.0/0"
97+
gateway_id = aws_nat_gateway.nat_gateway.id
98+
}
99+
100+
tags = {
101+
Name = "Private-Route-Table-${var.environment}"
102+
Terraform = "true"
103+
Environment = var.environment
104+
}
105+
}
106+
107+
resource "aws_route_table_association" "private_route_table_association" {
108+
count = length(aws_subnet.private_subnet)
109+
subnet_id = aws_subnet.private_subnet[count.index].id
110+
route_table_id = aws_route_table.private_route_table.id
111+
}
112+

0 commit comments

Comments
 (0)