EC2 - Load Balancing and Auto scaling

Load balancing and Auto scaling


Let's suppose we have deployed our web application to the EC2 instance. And all of the sudden, our app. got viral and previously 10k daily users became 1 million. So we want our infrastructure to use less resources when there is little or no user, more when there is high load or spike, it somehow must adjust itself in a way to withstand to this load. This is when auto scaling comes into play.



Load balancer - is a proxy that allows you to distribute connections to servers. It provides fault tolerance and high availability. Load balancer can be front of  EC2,ECS, other load balancers, lambda functions etc.

There are 3 types of load balancer in AWS:

  • Application load balancer (operates at Application layer (L7) http, https etc.)
  • Network load balancer (operates at IP layer (L4) -  TCP, TLS, UDP etc.) 
  • Gateway load balancer (uses Geneva protocol, balances firewalls,IDS/IPS, operates at layer 3 )


Auto scaling - allows you to automatically and terminate EC2, ECS, EKS instances based on your needs. It maintains availability allows you to scale easily. We need to define our EC2 template, so each time when auto scaler needs to create new EC2 instance, it will use that. And there is one more thing I want to add on top of that, auto scaler needs some information when to create, when to terminate instances. Based on the CloudWatch data and auto scaling policy we define, it will scale the system.

Types of Auto scaling:

Manual - make changes to AutoScaler manually
Dynamic - automatically scales based on demand (request size, CPU usage etc.)
Predictive - uses Machine learning to predict 
Scheduled - scales based on schedule (date/time)



Let's create auto scaler and connect it to the elastic load balancer

  1. First let's create own security group which will be very handy later on


  2. Add allow 80 and 22 ports to inbound rules.





  3. After that we should create a launch template which auto scaler will use it to create EC2 instances.








  4. Use amazon AMI and don't forget to select our security group we just created. 











  5. Add the script we used to create simple webpage in previous tutorial (https://www.blogger.com/u/1/blog/post/edit/6922501496740215722/8300358555009620696)






  6. Let's create auto scaling group.






  7. Use the launch template we just created.




  8. Adjust the minimum and maximum capacity based on the need




  9. We need to create a target group, so that load balancer will know which targets it will redirect traffic to. And for the auto scaler where to register targets to.




  10. Specify target group details



  11. And lastly we should create load balancer itself.




    Here select the name of target group !


  12. Okay, now let's connect our load balancer to auto scaling group too.






  13. Finally we are able to see results of our efforts here, 
























Comments

Popular posts from this blog

Identity Access Management - How to create user in IAM ?!

AWS pricing fundamentals

IAM - Roles