Skip to content

Commit a9bd5bf

Browse files
committed
Changes in terraform files to ensure a working network and DNS of ALB
1 parent 99b8a98 commit a9bd5bf

6 files changed

Lines changed: 102 additions & 58 deletions

File tree

terraform_infra/frontend_asg.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ data "aws_ami" "ubuntu" {
44

55
filter {
66
name = "name"
7-
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
7+
values = ["ubuntu/images/hvm-ssd-gp3/ubuntu-noble-24.04-amd64-server-*"]
88
}
99

1010
filter {
Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,52 @@
11
#!/bin/bash
22
set -e
33
exec > /var/log/user-data.log 2>&1
4-
sudo apt update && sudo apt upgrade -y
4+
5+
date
6+
7+
ip addr
8+
9+
ip route
10+
11+
nslookup security.ubuntu.com
12+
13+
curl http://security.ubuntu.com
14+
15+
sleep 60
16+
17+
curl http://security.ubuntu.com
18+
19+
echo "Waiting for apt repositories..."
20+
21+
until sudo apt update; do
22+
echo "apt update failed. Retrying in 10 seconds..."
23+
sleep 10
24+
done
25+
26+
sudo apt upgrade -y
527
sudo apt install docker.io -y
628
sudo apt install git -y
7-
sudo mkdir -p /usr/local/lib/docker/cli-plugins && sudo curl -SL https://github.com/docker/compose/releases/latest/download/docker-compose-linux-x86_64 -o /usr/local/lib/docker/cli-plugins/docker-compose && sudo chmod +x /usr/local/lib/docker/cli-plugins/docker-compose
29+
30+
sudo mkdir -p /usr/local/lib/docker/cli-plugins
31+
32+
sudo curl -SL https://github.com/docker/compose/releases/latest/download/docker-compose-linux-x86_64 \
33+
-o /usr/local/lib/docker/cli-plugins/docker-compose
34+
35+
sudo chmod +x /usr/local/lib/docker/cli-plugins/docker-compose
36+
837
sudo systemctl start docker
938
sudo systemctl enable docker
10-
usermod -aG docker ubuntu
39+
40+
sudo usermod -aG docker ubuntu
41+
1142
cd /home/ubuntu
43+
1244
git clone https://github.com/Lavanya-GOW/terraform-aws-three-tier-architecture
45+
1346
cd /home/ubuntu/terraform-aws-three-tier-architecture
47+
1448
docker --version
15-
sed -i "s/^IMAGE_TAG=.*/IMAGE_TAG=latest" .env
16-
docker compose pull
17-
docker compose up -d
49+
50+
sed -i "s/^IMAGE_TAG=.*/IMAGE_TAG=latest/" .env
51+
52+
docker compose up -d --build
Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,52 @@
11
#!/bin/bash
22
set -e
33
exec > /var/log/user-data.log 2>&1
4-
sudo apt update && sudo apt upgrade -y
4+
5+
date
6+
7+
ip addr
8+
9+
ip route
10+
11+
nslookup security.ubuntu.com
12+
13+
curl http://security.ubuntu.com
14+
15+
sleep 60
16+
17+
curl http://security.ubuntu.com
18+
19+
echo "Waiting for apt repositories..."
20+
21+
until sudo apt update; do
22+
echo "apt update failed. Retrying in 10 seconds..."
23+
sleep 10
24+
done
25+
26+
sudo apt upgrade -y
527
sudo apt install docker.io -y
628
sudo apt install git -y
7-
sudo mkdir -p /usr/local/lib/docker/cli-plugins && sudo curl -SL https://github.com/docker/compose/releases/latest/download/docker-compose-linux-x86_64 -o /usr/local/lib/docker/cli-plugins/docker-compose && sudo chmod +x /usr/local/lib/docker/cli-plugins/docker-compose
29+
30+
sudo mkdir -p /usr/local/lib/docker/cli-plugins
31+
32+
sudo curl -SL https://github.com/docker/compose/releases/latest/download/docker-compose-linux-x86_64 \
33+
-o /usr/local/lib/docker/cli-plugins/docker-compose
34+
35+
sudo chmod +x /usr/local/lib/docker/cli-plugins/docker-compose
36+
837
sudo systemctl start docker
938
sudo systemctl enable docker
10-
usermod -aG docker ubuntu
39+
40+
sudo usermod -aG docker ubuntu
41+
1142
cd /home/ubuntu
43+
1244
git clone https://github.com/Lavanya-GOW/terraform-aws-three-tier-architecture
45+
1346
cd /home/ubuntu/terraform-aws-three-tier-architecture
47+
1448
docker --version
15-
sed -i "s/^IMAGE_TAG=.*/IMAGE_TAG=latest" .env
16-
docker compose pull
17-
docker compose up -d
49+
50+
sed -i "s/^IMAGE_TAG=.*/IMAGE_TAG=latest/" .env
51+
52+
docker compose up -d --build

terraform_infra/security_groups.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ resource "aws_security_group" "frontend_sg" {
104104

105105
ingress {
106106
description = "Allow HTTP from external ALB"
107-
from_port = 5000
108-
to_port = 5000
107+
from_port = 80
108+
to_port = 80
109109
protocol = "tcp"
110110
security_groups = [aws_security_group.external_alb_sg.id]
111111
}

terraform_infra/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ variable "key_name" {
8787
variable "target_group_port" {
8888
description = "The port for the target group"
8989
type = number
90-
default = 5000
90+
default = 80
9191
}
9292

9393
variable "enable_deletion_protection" {

terraform_infra/vpc.tf

Lines changed: 16 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,12 @@ resource "aws_nat_gateway" "main" {
103103

104104
resource "aws_route_table" "public" {
105105
vpc_id = aws_vpc.main.id
106-
107-
route {
108-
cidr_block = var.route_table_cidr
109-
gateway_id = aws_internet_gateway.main.id
110-
}
111-
112106
}
113107

114108
resource "aws_route" "public" {
115109
route_table_id = aws_route_table.public.id
116110
gateway_id = aws_internet_gateway.main.id
117-
destination_cidr_block = "11.0.0.0/16"
111+
destination_cidr_block = "0.0.0.0/0"
118112
}
119113

120114
resource "aws_route_table_association" "public" {
@@ -126,71 +120,51 @@ resource "aws_route_table_association" "public" {
126120
}
127121

128122
resource "aws_route_table" "frontend" {
129-
count = var.enable_nat_gateway ? length(var.availability_zones) : 0
123+
count = length(var.availability_zones)
130124
vpc_id = aws_vpc.main.id
131-
132-
tags = merge(
133-
var.tags,
134-
{
135-
Name = "${var.environment}-${var.project}-frontend-rt-${count.index + 1}"
136-
Tier = "frontend"
137-
}
138-
)
139125
}
140126

141127
resource "aws_route" "frontend_nat" {
142-
count = var.enable_nat_gateway ? length(var.availability_zones) : 0
128+
count = length(var.availability_zones)
143129
route_table_id = aws_route_table.frontend[count.index].id
144-
destination_cidr_block = "12.0.0.0/16"
145-
nat_gateway_id = var.single_nat_gateway ? aws_nat_gateway.main[0].id : aws_nat_gateway.main[count.index].id
130+
destination_cidr_block = "0.0.0.0/0"
131+
nat_gateway_id = aws_nat_gateway.main[count.index].id
146132
}
147133

148134
resource "aws_route_table_association" "frontend" {
135+
149136
count = length(var.availability_zones)
150137
subnet_id = aws_subnet.frontend[count.index].id
151-
route_table_id = var.enable_nat_gateway ? aws_route_table.frontend[count.index].id : null
138+
route_table_id = aws_route_table.frontend[count.index].id
139+
152140
}
153141

154142
resource "aws_route_table" "backend" {
155-
count = var.enable_nat_gateway ? length(var.availability_zones) : 0
143+
count = length(var.availability_zones)
156144
vpc_id = aws_vpc.main.id
157-
158-
tags = merge(
159-
var.tags,
160-
{
161-
Name = "${var.environment}-${var.project}-backend-rt-${count.index + 1}"
162-
Tier = "backend"
163-
}
164-
)
165145
}
166146

167147
resource "aws_route" "backend_nat" {
168-
count = var.enable_nat_gateway ? length(var.availability_zones) : 0
148+
count = length(var.availability_zones)
169149
route_table_id = aws_route_table.backend[count.index].id
170-
destination_cidr_block = "13.0.0.0/16"
171-
nat_gateway_id = var.single_nat_gateway ? aws_nat_gateway.main[0].id : aws_nat_gateway.main[count.index].id
150+
destination_cidr_block = "0.0.0.0/0"
151+
nat_gateway_id = aws_nat_gateway.main[count.index].id
172152
}
173153

174154
resource "aws_route_table_association" "backend" {
155+
175156
count = length(var.availability_zones)
176157
subnet_id = aws_subnet.backend[count.index].id
177-
route_table_id = var.enable_nat_gateway ? aws_route_table.backend[count.index].id : null
158+
route_table_id = aws_route_table.backend[count.index].id
159+
178160
}
179161

180162
resource "aws_route_table" "database" {
181163
vpc_id = aws_vpc.main.id
182-
183-
tags = merge(
184-
var.tags,
185-
{
186-
Name = "${var.environment}-${var.project}-database-rt"
187-
Tier = "database"
188-
}
189-
)
190164
}
191165

192166
resource "aws_route_table_association" "database" {
193167
count = length(var.availability_zones)
194168
subnet_id = aws_subnet.database[count.index].id
195169
route_table_id = aws_route_table.database.id
196-
}
170+
}

0 commit comments

Comments
 (0)