In this section, you will execute commands to connect from your local machine to the Bastion Host, and then from the Bastion Host to the Backend server to install and run the application.
Note: The commands should be executed sequentially. Ensure you have successfully connected in the previous step before proceeding to the next one.
This is the first step, connecting from your computer to the Bastion Host in the Public Subnet.
project-bastion-host-ec2.ec2-user.project-keypair.pem file you downloaded when creating the EC2 instance.
Now that you are inside the Bastion Host, we need to prepare to connect to the Backend server.
Step 1: Recreate the key pair file on the Bastion Host.
Open a text editor like nano and paste the content of the .pem file from your local machine.
nano project-keypair.pem

Step 2: Set the correct permissions for the key file. SSH requires the key file to be protected; otherwise, it will refuse the connection.
chmod 400 project-keypair.pem

Step 3: SSH into the Backend.
Use the Private IPv4 address of project-backend-ec2.
ssh -i project-keypair.pem ec2-user@<Private_IP_of_Backend_EC2>

Once inside the Backend server, install the necessary tools.
Update the system:
sudo yum update -y

Install Git:
sudo yum install -y git

Clone the project source code from GitHub:
git clone https://github.com/tuilatri/weather-application.git

Navigate into the backend directory:
cd weather-application/backend

Install Node.js:
sudo dnf install -y nodejs

Install project dependencies:
npm install

Configure environment variables:
nano .env

Start the application:
npm run start

Test the application directly on the server:
Open another terminal window in MobaXterm, connect again, and then use the curl command to test.
curl http://localhost:3000/

After the Backend is operational, you need to get the Backend’s CloudFront URL and update it in the Frontend’s configuration file on S3.
Step 1: Get the Backend’s CloudFront Distribution Domain Name. Go to the CloudFront service in the AWS Console, find and copy the Domain Name of the distribution pointing to the backend.
Step 2: Access the S3 bucket containing the Frontend source code. Go to the S3 service and select the bucket that holds the frontend source code.
Step 3: Open and edit the api.js file.
Find the api.js file (usually in a js folder or similar), select the file, and click Edit.
Step 4: Update the API address. In the file’s content, find the line declaring the API variable and paste the Backend’s CloudFront URL. Then click Save changes.
// Change the value of this variable
const API_URL = 'https://your-cloudfront-backend-domain.cloudfront.net';

Step 5: Wait for the Frontend’s CloudFront to update.
After you save the api.js file, the CloudFront serving the Frontend will need a few minutes to clear the old cache and update to the new version of the file. Please wait about 5-10 minutes before checking the website again.
Waiting for CloudFront to update
If you update the code on GitHub but the changes do not appear on the web, follow these steps on the Backend server:
Pull the latest code:
git pull origin main
Find the running Node.js process:
ps aux | grep node
Stop the old process using its PID:
kill -9 <PID>
Real-world example:
[ec2-user@ip-10-0-144-101 backend]$ ps aux | grep node
ec2-user 3117 0.0 6.6 1127836 64896 pts/1 Sl+ 10:15 0:00 node server.js
[ec2-user@ip-10-0-144-101 backend]$ kill -9 3117
Restart the application:
npm run start