DEV Community

Cover image for Creating a web application using AWS Auto Scaling and Elastic Load Balancer (ELB)
Uduakabasi Umo-Odiong
Uduakabasi Umo-Odiong

Posted on

Creating a web application using AWS Auto Scaling and Elastic Load Balancer (ELB)

Overview
Create a highly available and scalable web application using AWS Auto Scaling and Elastic Load Balancer(ELB).

Introduction
One of the advantages of working with the cloud is autoscaling, which automatically changes your infrastructure and resources to meet the current level of demand. Another advantage is load balancing which is needed to help control and regulate the traffic demanding your resources. Having a web application with these features makes room for optimal performance and efficiency so walk with me let's create one together.

Autoscaling
Autoscaling is a cloud computing feature that allows an application to automatically scale its resources up or down based on changing workload demands. This means that as the number of requests or traffic increases, the cloud provider automatically adds more instances or resources to handle the market, and conversely, as the demand decreases, it removes unnecessary resources to save costs.

Load Balancing
A load balancer is a software or hardware component that distributes incoming network traffic across multiple servers or instances to improve responsiveness, reliability, and scalability. It acts as a single entry point for incoming traffic. It routes it to the available server with the lowest load, ensuring that no single server is overwhelmed and becomes a bottleneck.

AWS Ec2 Instance for Web Application
AWS EC2 (Elastic Compute Cloud) is a service that provides scalable and reliable computing resources for web applications.

Prerequisite

  1. An AWS account
  2. Basic knowledge of VPC and subnet
  3. Script for the web application

Steps

Step 1: Create and Configure your network for the Ec2 Instance

i. Log into your AWS console, use the search bar to search for VPC and click on it.

Image description

ii. Create your network using the VPC service

Image description

iii. Create two public subnets in two different availability zones

Image description

iv. Create one public route table

Image description

v. Create an Internet Gateway (IGW)

Image description

vi. Attach the Internet gateway to the VPC you created

Image description

vii. Select the route table created and configure the routes and subnet association

Image description

viii. Edit routes and add routes then select Internet gateway

Image description

ix. Select Subnet association, Edit subnet association then select both subnets created and save changes.

Image description

Step 2: Create your Servers using AWS EC2 service
i. Use the search bar to search for Ec2 and click on it.

Image description

ii. Create 2 servers for the two subnets you created by clicking on launch instance

Image description

iii. Set the name of your first server and leave everything after as default until you get to network settings where you select the VPC you created and select one subnet out of the two you created.

Image description

iv. After network settings set your security group inbound details to SSH from anywhere and HTTP from anywhere because we are working with a public subnet

Image description

v. Leave everything after the security group as default and once you get to Advanced details select it and scroll down to user data, this is where you input the script for your web application and then click on launch instance.

Image description

vi. Create another server for your second public subnet and follow the same steps to launch your instance.

Image description

Step 3: Set up your Elastic Load Balancer

i. Use the search bar to search for Load balancer and click on it Or Search Ec2, click on it and the left panel of services under Ec2 Scroll down to load balancers

Image description

ii. Click on create the load balancer

Image description

iii. We are working on a web application so select the application load balancer and click on create

Image description
Image description

iv. Input the name of the load balancer, It is for internet users so leave the scheme as internet facing and leave the IP address as default

Image description

v. Under the network mapping, select the VPC you created then under availability zones select both.

Image description

vi. Create a new security group, select the VPC you created and for the inbound rules add HTTP from anywhere. Leave every other thing as default and click on Create a security group.

Image description

Image description

Image description

vii. Go back to set up and at security group select the security group you just created

Image description

viii. Under listening and routing, for listening select HTTP as protocol and Port 80. Then create a target group for where the traffic will be channeled to

Image description

ix. After clicking Create target group select the instance and leave other details as default and HTTP where applicable

Image description

x. Click on next then select both instances created to register them as target

Image description

xi. Include the selected instances as pending and they will appear under review targets.

Image description

xii. Click on Create Target Group to create

Image description

xiii. Go back to setting up the load balancer and select the target group you just created

Image description

xiv. Scroll down and Click on Create the load Balancer to complete the set-up

Image description

xv. To test your load balancer, scroll down copy the DNS and paste it into another tab. After it opens refresh to see if it connects you to another server. Then you know that your load balancer has been set up properly.

Image description

xvi. Paste the link in a new tab and click enter. The details of the first instance will appear.

Image description

Xvii. Click refresh, and the load balancer will take you to the second server. This means the Load balancer setup is successful.

Image description

Step 4: Set up your Auto Scaling

i. Use the search bar to search for Auto-Scaling and click on it Or Search Ec2, click on it and the left panel of services under Ec2 Scroll down to Auto-Scaling.

Image description

ii. To continue you will need to set up an AMI so go back to your instances and select the first server then go to actions, click on images and template then select create image. Input the name of your image and go on to create the image.

Image description

iii. Under the panel on the left-hand side of your console, scroll down and select AMI to confirm if your AMI is up and available

Image description

iv. Go back to create your auto-scaling group

Image description

v. Enter the name of the auto-scaling group then scroll down and click on Create launch a launch template which will contain your AMI created earlier

Image description

vi. While creating the launch template, input the name and under Amazon Machine Image select owned by me and click on the image you created then scroll down to security group and select already existing which is the one you created earlier then.

Image description

vii. Go back to Creating your Auto-Scaling group and refresh the launch template so select the template you just created

Image description

viii. Click on next to continue

xi. Leave everything before network on default and under network select the VPC you created earlier and under availability zones select the availability zones you created then click on next

Image description

x. Attach the load balancer you created then select the target group. Leave every other as default and select and click on next to continue.

Image description

xi. Set your group size and scaling capacity based on what you want

Image description

Image description

Image description

x. Leave other settings on default and click Create an Auto Scaling group

Image description

xi. Stress test your auto-scaling by connecting your instance to the Command Line

Image description

xii. Confirm the connection

Image description

xiii. Install the stress utility using the command: sudo yum install stress -y

Image description

xiv. Run the Stress Test:
Use the stress command to generate load by running the stress command to simulate high CPU load for a set period (e.g. 100 seconds use the command "stress -c 1 -i 1 -m 1 --vm-bytes 128M -t 100s")

Image description

Xv. Click on the auto-scaling group as well as instances to see the additional instance that has been created because of the stress command.

Image description

Image description

Conclusion
In conclusion, Auto scaling and Load Balancing are important concepts that every cloud/DevOps Engineer or individual learning cloud computing needs to know due to the value they bring to any Cloud project.

Auto-scaling would help you adjust your instance to the volume of traffic coming in. If there's an increase in traffic, this service creates more instances. With a reduction in traffic, well, you guessed right, it will help reduce the instances, and this is very effective for cost optimization.

On the other hand load balancing helps to navigate, control and balance the traffic coming into an infrastructure to ensure smooth flow. From the explanation, it is a must-know and should be applied while creating your infrastructure so the steps highlighted above should be carefully followed when implementing auto-scaling and load balancing.

Top comments (0)