What is Autoscaling in AWS?
Autoscaling is nothing but an extension to load balancing in AWS.
We use load balancing along with the Autoscaling feature.
To recap,
The load balancer receives incoming requests/traffic and distributes the requests/traffic to registered target instances.
What is the Advantage of a Load balancer?
If one target instance is down/unhealthy, we can still get the application from other target instances.
Also, there is no limit of instances while attaching to the load balancer.
Companies use Load balancers & Autoscaling as a part of their business.
Example:
Let's consider that CompanyA is using a Load balancer
How many instances can CompanyA use as a part of the load balancer?
When there is an increase/decrease in traffic, how the company can handle this scenario?
No of the instances we attach shouldn't be static, considering the business as a priority.
No of the instances we attach should be purely based on the traffic
When traffic increase-we, need more no of EC2 Instances
When traffic decrease-we, need less no of EC2 Instances
No of the instances we attach should be dynamic.
Based on the traffic, the scaling should happen dynamically.
The auto-scaling feature helps us automatically scale up the infrastructure and scale down the infrastructure based on the traffic.
The following are the sequence of steps we need to perform while working with Autoscaling.
Step1 - Create a load balancer
Step2 - Create Launch configuration
Step3 - Create Topic in SNS(Simple Notification Service)
Step4 - Create Autoscaling group(min/max)
Step5 - Create Alarm in CloudWatch (This is condition)
Step6 - Add policy in Auto Scaling (This is an action for the condition)
Step1 - Create load balancer(For WebServers, we use Classic Load Balancer)
Specify Load balancer name
Create security group
Specify the security group name
Open SSH(22) and HTTP(80) ports for communication
Configure Health Check
Create
What is Launch configuration & Why do we need Launch configuration?
When traffic increases, Autoscaling should add the new EC2 instances automatically.
What happens when there is unexpected traffic on any special day/occasion? For this, we have business analysts who give instructions to the infrastructure team.
Based on the previous statistics, business analysts develop some plans and give their inputs to the internal teams accordingly to be prepared for any unexpected traffic.
When traffic decreases, Autoscaling should remove the EC2 instances automatically.
There is no manual process here, especially while adding or removing EC2 instances.
The most important questions as a part of Autoscaling are
What would be the RAM for the new EC2 instances?
What would be the HDD for the new EC2 instances?
What would be the Processor for the new EC2 instances?
What would be the OS for the new EC2 instances?
What are the ports we need to open for new EC2 instances?
Should we install any software while the EC2 instance gets created?
We need to pre-define all the above as part of the Launch configuration so that auto-scaling helps us automatically scale up the infrastructure and scale down the infrastructure based on the traffic.
Step2 - Create Launch configuration (This is a part of Autoscaling where we can see this from the Autoscaling Menu)
Specify the launch configuration name
Choose the AMI Image/ID correctly(Autoscaling is going to use while launching new EC2 instances as a part of scaling up the infrastructure)
Choose the instance type correctly(Autoscaling is going to use while launching new EC2 instances as a part of scaling up the infrastructure)
Since this web server in our example, go to advanced details - user data - use the bootstrap script here(Autoscaling is going to use while launching new EC2 instances as a part of scaling up the infrastructure)
Storage - EBS volumes - Select the Size correctly(Autoscaling is going to use while launching new EC2 instances as a part of scaling up the infrastructure)
Security groups - We need to assign a security group correctly(Autoscaling is going to use while launching new EC2 instances as a part of scaling up the infrastructure)
Key pair - We need to choose the key pair correctly(Autoscaling is going to use while launching new EC2 instances as a part of scaling up the infrastructure)
The purpose of the key pair is to get connected to EC2 Machine.
Step3 - Create Topic in SNS(Simple Notification Service)
What is Topic & Why do we need SNS?
The topic is nothing but a group where the entire group can receive email notifications.
SNS stands for Simple Notification Service
When the traffic increases, auto-scaling happens as per the configuration. We need to receive email notifications to scale up the infrastructure and scale down the infrastructure in this scenario.
Go to Application Integration - Simple Notification Service
Click on Topics - Create Topic - Choose standard
Specify Topic Name -
Specify Topic Display name -
Create
Now we need to add emails id's to the Topic.
Go to the Topic - Look for the subscription option
Click on create subscription
Protocol - Email
Endpoint - Specify email id OR group of email ids to receive notifications
Create subscription
We need to confirm email as a part of validation to receive notifications as this is mandatory.
Step4 - Create Autoscaling group(min/max)
We need to create an Autoscaling group based on the Launch configuration that we have created.
Select Launch configuration - Go to Actions - Create Autoscaling group
Specify Autoscaling group name
Choose VPC correctly(This is where Auto scaling happens)
Subnet - Choose this correctly(Subnet is nothing but a partition in your AWS Region. You may have more than one subnet, hence choose the subnet correctly so that the Autoscaling happens in the given subnet partition)
Load balancing - Attach to an existing load balancer - Choose from Classic Load balancers - Select your Load balancer correctly so that whenever Auto scaling happens, automatically EC2 Instances would be a part of Load balancer by default.
Group size - (Desired-1 ) (Min -1) (Max -1) We can also use any capacity, but as a part of learning/training, a minimum is recommended. At the company's we follow the size based on the statistics of the previous sales/business.
(Desired is nothing but to start with) (Minimum is minimum instances) (Maximum is maximum instances)
Create Autoscaling group
Check the status of the Autoscaling group
Add notifications - Select topic where we need to receive notifications
Add tags appropriately
Review
Create
Creation may take some time, depending on the configuration we have specified. Keep checking the status, and we can proceed with the next step when the status is
Step5 - Create Alarm in CloudWatch (This is condition)
How do we measure the traffic for the auto-scaling?
When Traffic increases/Traffic decreases, what is the condition?
Creating an alarm is nothing defining a condition when there is traffic increase/decrease.
We have a metric known as CPUUtilization provided by AWS
If CPUUtilization>70%, then create new EC2 or create more EC2 based on the business requirement
If CPUUtilization<30%, then remove EC2 or remove more EC2
In this scenario, we have to create 2 Alarms in CloudWatch
Alarm1-CPUUtilization-GTE70
Alarm2-CPUUtilization-LTE30
In the above step, we have created 2 conditions.
Now, what is the action to take for every condition is nothing but adding policy in Autoscaling.
Step6 - Add policy in Auto Scaling (This is an action for the condition)
In this scenario, we have to create 2 policies from the Dynamic scaling policy
Policy1 is for Alarm1-CPUUtilization-GTE70 (Adding EC2 Instances)
Policy2 is for Alarm2-CPUUtilization-LTE30 (Removing EC2 Instances)
Once the policies are created, we can take appropriate action accordingly
The Application should not go down no matter what happens.
Deleting process:
============
1)Delete AutoScaling
2)Delete launch configuration (Instances will be terminated automatically)
3)Delete Load balancer
4)Delete Topic
5)Delete Alarm