In this guide, we will walk you through the process of deploying a Book Management app, which is a REST API built using express.js, to Amazon Web Services (AWS). The app will be deployed in an Auto-Scaling Group behind an Application Load Balancer (ALB), and it will be managed by systemd. Additionally, we'll show you how to add an endpoint that returns the EC2 instance's IP address.
Before you begin, make sure you have the following:
- An AWS account
- Basic knowledge of AWS services
- Git installed on your local machine
-
Create a public GitHub repository for your project if you haven't already.
-
Clone the repository to your local machine using
git clone. -
Navigate to the app's directory and build the app by running:
npm install npm run dev
-
Package the app using the following command:
tar czvf app.tar.gz package.json package-lock.json dist
-
Go to your repository on GitHub and click on the "Releases" tab.
-
Click on the "Draft a new release" button.
-
Provide a version tag and release title, then upload the app.tar.gz file you created earlier.
-
Publish the release.
- Go to the AWS Management Console.
- Navigate to the EC2 Dashboard.
- In the left sidebar, under "Security Groups," click on "Create Security Group."
- Configure inbound rules to allow HTTP/HTTPS traffic (port 80 and 443)
- configure outbound rules as needed.
- Create the security group.
-
Create a bash script named user_data.sh.
-
Add the following content to the script:
#!/bin/bash
sudo apt update
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - &&\
sudo apt-get install -y nodejs
npm install -g npm
cd /home/ubuntu
sudo git clone https://github.com/MostafaTahboub/Book-Management-App.git Book-Management-App
cd Book-Management-App
npm install express
echo "[Unit]
Description=Book Management App
After=network.target
[Service]
ExecStart=/usr/bin/node ./dist/index.js
WorkingDirectory=/home/ubuntu/Book-Management-App
Restart=always
User=ubuntu
[Install]
WantedBy=multi-user.target" | sudo tee /etc/systemd/system/book-management-app.service
sudo systemctl enable book-management-app
sudo systemctl start book-management-app
- please make sure to replace a paths with your needs .
- Navigate to the EC2 Dashboard.
- Under "Instances," click on "Launch Templates."
- Click on the "Create launch template" button.
- Configure the template, including instance type, key pair, and user data.
- In the "Advanced Details" section, add a user data script (the user_data.sh script you created earlier).
- Create the launch template.
- Navigate to the EC2 Dashboard.
- Under "Auto Scaling Groups," click on "Create Auto Scaling group."
- Select the launch template you created.
- Configure the Auto Scaling group settings, including desired capacity and scaling policies.
- Add tags as needed.
- Review and create the Auto Scaling group.
-
In your app code, create a new route that returns the instance's private IP address.
-
For example, using express.js:
app.get('/get-ip', (req, res) => {
const ip = req.socket.remoteAddress;
res.json({ ip });
});
Congratulations! You have successfully deployed your Book Management app to AWS using Auto Scaling and Load Balancing. Your app is now accessible through the ALB and can handle varying levels of traffic while ensuring high availability.
Feel free to customize and expand this deployment process according to your project's needs. If you encounter any issues, refer to the AWS documentation for further assistance.