Today I would like to introduce the most common architecture in AWS – load balancer and auto scaling. When you are the beginner in AWS, this article is definitely suitable for you. When we are using traditional approach, we will be in trouble if there are suddenly high traffic. By using load balancer, the incoming traffic is distributed to multiple EC2. Also, by using auto scaling, the group of EC2 could increase or decrease when it meets the condition. I assume that you all had AWS account already. If you don’t have, please visit here to register a 1 year free-tier account.
There are 3 types of load balancers in AWS.
1. Application Load Balancer
An Application Load Balancer functions at the application layer, the seventh layer of the Open Systems Interconnection (OSI) model. After the load balancer receives a request, it evaluates the listener rules in priority order to determine which rule to apply, and then selects a target from the target group for the rule action. It is suitable for load balancing HTTP and HTTPS traffic.
2. Network Load Balancer
A Network Load Balancer functions at the fourth layer of the Open Systems Interconnection (OSI) model. It can handle millions of requests per second. After the load balancer receives a connection request, it selects a target from the target group for the default rule. It is suitable for ultra-high performance, TLS offloading at scale, centralized certificate deployment, support for UDP, and static IP addresses for your application.
3. Classic Load Balancer
A Classic Load Balancer is the legacy Elastic Load Balancer. It distributes incoming application traffic across multiple EC2 instances in multiple Availability Zones. This increases the fault tolerance of your applications. Elastic Load Balancing detects unhealthy instances and routes traffic only to healthy instances. It is suitable for an existing application running in the EC2-Classic network.
In this tutorial, we will use application load balancers for demonstration.
In this tutorial, it is up to you to choose any region. For your reference, I will choose London. You could choose the region which is close to you.
Create Auto Scaling Group
First of all, we have to create an instance, just search EC2 on the bar.
On the left hand side menu, move down and choose Launch Configuration under Auto Scaling. Then click launch configuration.
If you are using free tier and don’t want to get any charge, please check the free tier only box and then select Amazon Linux 2 AMI.
Nothing change, click next.
In this page we type the configuration name first, then scroll down and click “Advanced Details”. Put the below script into User Data:
#!/bin/bash yum update -y yum install httpd -y echo "<html> <body> <center> <h1>Welcome to Cloud Tutorial!</h1> </center> </body> </html>" > /var/www/html/index.html systemctl start httpd systemctl enable httpd
This is the startup script to let the EC2 shows something when we browse it through http request. Then click Next.
Nothing change in this page, just Click Next.
Also, we don’t have change in this page, just click Next.
Then in this page, we have to create a new rule like above figure. If you know your PC IP, then just type on the source IP. If you don’t know, just leave it 0.0.0.0/0 which means open for everyone. This is just a demonstration so it doesn’t matter. Then click Review and Launch.
Click Launch after reviewing all the configurations.
If you have your own key, you could choose existing key. But if you don’t have, just create a new key pair and download it, then click launch instance.
Click Create an Auto Scaling Group.
In this page, we assign the name of the group. Also, we set starting group size as 2 in order to see the effect faster. Then we choose all the subnet and click Next.
Keep it by default, we could directly click “Review”.
Finally click Create Auto Scaling Group.
In instance page, we could see there are 2 instances during creation.
Create Application Load Balancer
In EC2 instance left hand side menu, move down and click Load Balancers.
Click Create Load Balancer.
Choose Application Load Balancer and click Create.
Type the name for load balancer, check all the AZs, and then click Next.
Nothing change, click Next.
In this page we check the security group which is created previously for auto scaling, then click Next.
We assign a name for the target group, type the health check path as /index.html, and click Next.
In this page, we firstly check the two instances and click Add to registered.
Then we click Next to review.
Click Create and close.
When we wait for few minutes, the load balancer will show “active”. When it is ready, we copy the DNS name for testing.
When we browse the DNS name, we will be redirected to one of the instance and can see the welcome page.
In instance page, we could select one of the instance and click Actions->Instance State->Terminate.
Even though the instance is shutting down, but we still can see the page without any impact because all traffic are routed to the second instance.
This time we also terminate the second instance.
Then the page is totally down now.
However, thanks to the auto scaling, a new instance will automatically build within few minutes. The service resumes now.
Finally, you may want to clean up all the things.
In Auto Scaling Groups, choose the group we created and click Actions->Delete.
Similarly, we delete the launch configuration under auto scaling.
We delete the load balancers under load balancing.
Also we delete the target group under load balancing.
And finally we delete the security group under network & security.