I. Creating an Auto Scaling Group
- Signing in to your AWS account
- Creating an Auto Scaling Group
- Creating a Launch Template
- Configuring the Launch Template
- Adding Startup Script to Instances
II. Setting Up Load Balancers
- Creating a Load Balancer
- Configuring Load Balancer Settings
- Creating a Target Group
- Configuring Health Checks
- Setting Group Size
Before we dive into the deployment process, ensure you have the following:
- An AWS account
- Your application code was uploaded to GitHub in a public repo.
- Familiarity with basic AWS concepts
1. Signing in to your AWS account:
Begin by signing in to your AWS account from this link 👉[AWS].
2. Set general settings: 🔗
Select a region that is geographically closer to your target audience. In my case I chose London.
-
Creating an Auto Scaling Group:👥
4.1 From the right side choose: Auto Scalling Group
4.2 Click on Creat Auto Scaling Group:
4.3 Choose a unique name for your Auto Scaling group.
4.4 Create a Launch Template:
You can choose Launch Template if you have one, now we will create new one. The Launch Template contains instance configuration details.
4.5 Provide a descriptive name and useful description for your template.📜
4.6 Walkthrough on creating an optimal launch template to ensure smooth application deployment. I use this template, you may have to change something in it.
Use a key pair to securely connect to your instance. Ensure that you have access to the selected key pair before you launch the instance.
You can create Security group and add rules to allow specific traffic to reach your instance.
Scroll down to reach Advanced details
Scroll down to reach User data.
Write your script in the box.
This script is for my Express JS project, you can write any script you want, but you have to make sure the script is good, you can test it in alone instance.
- Updating and upgrading packages
- Installing Node.js and other dependencies
- Creating user accounts for security
- Downloading and deploying your app
#!/bin/sh
set -e
sudo apt update
sudo apt upgrade -y
# install nodejs repo to download the latest version then the node js
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install nodejs jq curl -y
# create app and github users, to run the application from them, without root, for security purposes
sudo useradd --system --create-home --shell /usr/sbin/nologin app
sudo useradd -g app --no-create-home --no-user-group --home-dir /home/app --shell /bin/bash github
sudo usermod --append --groups app github
# deploy the app
# Write the github acoount and repo in a variable
repo="SarahAbuirmeileh/Book-App"
# Creat a download URL to use it later
# Here we want to download a release which is ready to run.
download_url=$(curl "https://api.github.com/repos/$repo/releases/latest" | jq --raw-output '.assets[0].browser_download_url')
# Download the service file to run it as systemd service
curl -O "https://raw.githubusercontent.com/$repo/main/infrastructure/app.service"
# Adding the file to systemd and enable it, to run whenever the instance start
sudo mv app.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable app.service
# Creat a repository for the application, and download the app there, then dcompress the app and install the npm
sudo -u app sh -c "mkdir -p /home/app/app && cd /home/app/app && curl -LO $download_url && tar xzvf app.tar.gz && npm install --omit=dev"
sudo reboot
# Evrything is done !!!
Create the template.
4.7 Move back to the Auto Scaling group and choose your template.
Note: If your template didn’t appear, you may need to refresh the template.
4.8 Choose the latest version (optional), then click next.
4.9 Choose the Availability Zones
- It’s recommended to choose as possible as you can
- The number of Availability Zones may be different from one region to another.
Now let’s move to the next part.
II. Setting Up Load Balancers📦
Fine-tune Load Balancer settings to optimize traffic distribution and improve availability.
1.1 Creat a Load Balancer and choose the type of it.
You can choose ‘Attach to an existing load balancer’ if you already have one, now we’ll creat new one.
1.2 Write the name of your Load Balancer and choose the scheme.
Internet-facing is designed to distribute incoming traffic from the internet to multiple targets.
1.3 Creat target group
Configure a Target Group to manage routing traffic to instances effectively.
Choose the target group name
1.4 Let the VPC Lattice integration options to the default value and enable the Turn on Elastic Load Balancing health checks
1.5 Choose the Health check grace period according to your requirements.
Set up health checks to monitor the instances’ status and replace unhealthy instances automatically.
1.6 Choose the Group size : Desired, Minimum, Maximum.
- Determine the appropriate group size based on your application’s needs and ensure high availability.
- It’s recommended to have a minimum of 2 instances because if one fails there will be another one.
1.7 Enable instance scale-in protection (optional)
You can choose Scaling policies
1.8 You have to see something like this 😁
1.9 If you go to the EC2 instances you’ll see the your desired number of your instances is generated automatchly.
5.10 Go to your target group to check the health of your instances.
5.11 If you want to delete this work, you should start by deleting your Auto Scaling group.
Congratulations! By following this comprehensive guide, you’ve successfully learned how to deploy your application using AWS Auto Scaling Groups and Load Balancers. This process ensures high availability, fault tolerance, and efficient scaling for your applications. Now, you’re ready to take your AWS deployment skills to the next level and build robust, scalable applications.
Deploying applications on AWS can seem daunting, but with this step-by-step guide, you’re equipped with the knowledge needed to navigate the process seamlessly. Remember, practice makes perfect. Feel free to experiment with different settings and configurations to tailor the deployment to your specific project’s needs.