- Go to the VPC Dashboard in AWS Management Console.
- Click on "Create VPC".
- Choose the VPC-only option, and set the CIDR block to 192.168.0.0/16.
- Name the VPC, '
Lec1-Assignment-2-Teir-App'.
- 2 public (Lec1-Assignment-Public-Subnet-1 & Lec1-Assignment-Public-Subnet-2)
- 2 private (Lec1-Assignment-Private-Subnet-1 & Lec1-Assignment-Private-Subnet-2)
create interent gateway Lec1-Assignment-IGW to make public subnet access interent 'inbound and outbound'
create interent gateway My-Nat-Gateway to make private subnet access interent 'outbound' only, for download and get its package and tools
this nat-gateway exist in subnet Lec1-Assignment-Private-Subnet-2
- first route table to connect (Lec1-Assignment-Public-Subnet-1 & Lec1-Assignment-Public-Subnet-2) with
Lec1-Assignment-IGW - second route table to connect (Lec1-Assignment-Private-Subnet-1 & Lec1-Assignment-Private-Subnet-2) with
My-Nat-Gateway
-
create 2 public instances (Lec1-Assignment-instance-A & Lec1-Assignment-instance-B) each one have elastic public IP to be access to/by the interent easily and can connect using SSH with key-pair each one contain private id too, to be connect with other instance inside the network it contain nginx that run as a forwarding to the private proxy instance which is the engering of teir 2

-
create public proxy instance (Lec1-Assignment-Public-Proxy) it has elastic public IP to be access to/by the interent easily and can connect using SSH with key-pair it contain nginx that run as a load balance between the 2 public instance

-
create 2 private instances (Lec1-Assignment-instance-C & Lec1-Assignment-instance-D) each one have only private IP to be connect with other instance inside the network and can connect using SSH with key-pair

-
create private proxy instance (Lec1-Assignment-Private-Proxy) it has only private IP to be connect with other instance inside the network and can connect using SSH with key-pair it contain nginx that run as a load balance between the 2 private instance

test of public proxy to the 2 public instances then to private proxy finally to the 2 private instances
Allow inbound traffic: Type: HTTP, Protocol: TCP, Port Range: 80, Source: Anywhere (0.0.0.0/0, ::/0). Type: SSH, Protocol: TCP, Port Range: 22, Source: My IP (for secure access)// or anywhere.
- connect using SSH into each instance using the public IP address:
ssh -i 2-Teir-App.pem ec2-user@<public-ip-od-instance>- Install NGINX:
sudo yum update -y
sudo yum install nginx1 -y
sudo systemctl start nginx
sudo systemctl enable nginx- config the load balance
add following to
sudo vi /etc/nginx/nginx.conf
http {
upstream backend {
server <public-instance-1-private-ip>:80;
server <public-instance-2-private-ip>:80;
}
server {
listen 80;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}check the syntax error
sudo nginx -tapply the configuration by reload nginx
sudo systemctl reload nginxAllow inbound traffic: Type: HTTP, Protocol: TCP, Port Range: 80, Source: Anywhere (0.0.0.0/0, ::/0). Type: SSH, Protocol: TCP, Port Range: 22, Source: My IP (for secure access)// or anywhere.
- copy key-pem to a public instance
scp -i 2-Teir-App.pem 2-Teir-App.pem ec2-user@34.200.246.143:/home/ec2-user/- connect using SSH into each instance using the public IP address:
ssh -i 2-Teir-App.pem ec2-user@<public-ip-of-any-public-instance-has-the-key>
ssh -i 2-Teir-App.pem ec2-user@<public-ip-of-private-instance>- Install NGINX:
sudo yum update -y
sudo yum install nginx1 -y
sudo systemctl start nginx
sudo systemctl enable nginx- config the load balance
add following to
sudo vi /etc/nginx/nginx.conf
http {
upstream backend {
server <public-instance-1-private-ip>:80;
server <public-instance-2-private-ip>:80;
}
server {
listen 80;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}check the syntax error
sudo nginx -tapply the configuration by reload nginx
sudo systemctl reload nginxAllow inbound traffic: Type: HTTP, Protocol: TCP, Port Range: 80, Source: Anywhere (0.0.0.0/0, ::/0). Type: SSH, Protocol: TCP, Port Range: 22, Source: My IP (for secure access)// or anywhere.
- connect using SSH into each instance using the public IP address:
ssh -i 2-Teir-App.pem ec2-user@<public-ip-of-instance>- Install NGINX:
sudo yum update -y
sudo yum install nginx1 -y
sudo systemctl start nginx
sudo systemctl enable nginxadd following to sudo vi /etc/nginx/conf.d/forwarding.conf
server {
listen 80;
server_name _;
location / {
# Forward requests to the private proxy instance
proxy_pass http://192.168.3.139;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
add following to sudo vi /etc/nginx/nginx.conf
server {
listen 80;
server_name _;
location / {
# Forward requests to the private proxy instance
proxy_pass http://192.168.3.139; # Make sure this is the correct private IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}check the syntax error
sudo nginx -tapply the configuration by reload nginx
sudo systemctl reload nginxadd following to sudo vi /usr/local/bin/update-ip.sh
# Create an HTML page with the private IP address
echo "<!DOCTYPE html>" > /usr/share/nginx/html/index.html
echo "<html>" >> /usr/share/nginx/html/index.html
echo "<head><title>My Private EC2</title></head>" >> /usr/share/nginx/html/index.html
echo "<body>" >> /usr/share/nginx/html/index.html
echo "<h1>Private IP Address IN Private EC2 (Last Node)</h1>" >> /usr/share/nginx/html/index.html
echo "<p>Your private IP address is: <b>$PRIVATE_IP</b></p>" >> /usr/share/nginx/html/index.html
echo "</body>" >> /usr/share/nginx/html/index.html
echo "</html>" >> /usr/share/nginx/html/index.htmlRun the Script Again: Execute the updated script manually to verify that it works correctly:
sudo chmod +x /usr/local/bin/update-ip.sh
sudo /usr/local/bin/update-ip.shcheck the syntax error
sudo nginx -tapply the configuration by reload nginx
sudo systemctl reload nginx





