@@ -21,6 +21,22 @@ resource "aws_subnet" "public_subnet" {
21
21
}
22
22
23
23
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
+
24
40
resource "aws_internet_gateway" "ci_cd_demo_igw" {
25
41
vpc_id = aws_vpc. ci_cd_demo_vpc . id
26
42
@@ -46,4 +62,51 @@ resource "aws_route_table_association" "public_route_table_association" {
46
62
count = length (aws_subnet. public_subnet )
47
63
subnet_id = aws_subnet. public_subnet [count . index ]. id
48
64
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