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
- An AWS account
- Basic knowledge of VPC and subnet
- 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.
ii. Create your network using the VPC service
iii. Create two public subnets in two different availability zones
iv. Create one public route table
v. Create an Internet Gateway (IGW)
vi. Attach the Internet gateway to the VPC you created
vii. Select the route table created and configure the routes and subnet association
viii. Edit routes and add routes then select Internet gateway
ix. Select Subnet association, Edit subnet association then select both subnets created and save changes.
Step 2: Create your Servers using AWS EC2 service
i. Use the search bar to search for Ec2 and click on it.
ii. Create 2 servers for the two subnets you created by clicking on launch instance
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.
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
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.
vi. Create another server for your second public subnet and follow the same steps to launch your instance.
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
ii. Click on create the load balancer
iii. We are working on a web application so select the application load balancer and click on create
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
v. Under the network mapping, select the VPC you created then under availability zones select both.
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.
vii. Go back to set up and at security group select the security group you just created
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
ix. After clicking Create target group select the instance and leave other details as default and HTTP where applicable
x. Click on next then select both instances created to register them as target
xi. Include the selected instances as pending and they will appear under review targets.
xii. Click on Create Target Group to create
xiii. Go back to setting up the load balancer and select the target group you just created
xiv. Scroll down and Click on Create the load Balancer to complete the set-up
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.
xvi. Paste the link in a new tab and click enter. The details of the first instance will appear.
Xvii. Click refresh, and the load balancer will take you to the second server. This means the Load balancer setup is successful.
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.
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.
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
iv. Go back to create your auto-scaling group
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
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.
vii. Go back to Creating your Auto-Scaling group and refresh the launch template so select the template you just created
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
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.
xi. Set your group size and scaling capacity based on what you want
x. Leave other settings on default and click Create an Auto Scaling group
xi. Stress test your auto-scaling by connecting your instance to the Command Line
xii. Confirm the connection
xiii. Install the stress utility using the command: sudo yum install stress -y
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")
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.
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)