Auto Scaling in AWS is the practice of automatically adjusting computational resources based on the volume of user demand. In simpler terms, when your application experiences a spike in traffic, AWS automatically provisions more resources. Conversely, when the traffic subsides, AWS scales down to avoid unnecessary costs.
Why is Auto Scaling a Game-Changer?
Optimized Costs: Only pay for the resources you're using. Gone are the days of over-provisioning "just in case" or under-provisioning and risking performance.
High Availability: Ensures that your application remains accessible, even during unexpected traffic spikes. This translates to consistent user experience and satisfaction.
Effortless Management: No need for constant monitoring or manual adjustments. AWS takes the reins, allowing you to focus on what you do best - innovating and delivering value.
How Does AWS Auto Scaling Work?
Target Tracking: This is like your car's cruise control but for AWS resources. You define a desired metric (e.g., 50% CPU utilization), and AWS ensures it's maintained by scaling resources up or down.
Step Scaling: Think of this as conditional rules. For instance, "If CPU usage > 70%, launch 1 more instances."
Scheduled Scaling: As the name suggests, you can predefine scaling activities based on known usage patterns, like scaling up every weekday morning when traffic spikes.
Where Can You Use AWS Auto Scaling?
While commonly associated with EC2 instances, Auto Scaling isn't limited to them. AWS offers this functionality across various services like:
EC2 instances
DynamoDB tables
Aurora replicas
ECS tasks ... and many more!
Let's GO!
Creating an Auto Scaling group in the AWS Management Console involves several steps. I'll guide you through the process of setting up an Auto Scaling group that launches EC2 instances based on demand. For simplicity, I'll assume you already have a VPC and its related networking components set up, and you have an EC2 instance AMI ready.
Setting up an Auto Scaling Group (ASG):
Create a Launch Configuration:
Open the Amazon EC2 dashboard in the AWS Management Console.
In the left-hand navigation pane, under "Auto Scaling", Click on Auto Scaling Group and navigate to the right side you can see, Create Auto Scaling group
Choose “Create launch configuration”. Mention Auto Scaling Group Name
Choose the Template used in the previous blog https://kshitijaa.hashnode.dev/host-website-and-monitor-using-cloudwatch
Configure Network - it has chosen default VPC as per template, I have selected availability zones 1a and 1b
Instance type requirements are as per the template. Click on Next to continue.
Configuring Configure advanced options - Keeping everything as default.
-
Configure group size and scaling policies Keeping Desired Capacity-1, Min-1, Max-2
Scroll down to set the Target Tracking Policy to Launch a new instance when CPU utilization is more than 70%
Get notified by choosing an existing SNS topic for all change activities in the ASG
It is a best practice to add tags for your own reference, to identify launched instances through ASG
Review and click "Create Auto Scaling Group".
Auto Scaling has launched an instnace and it is running as expected!
Browse the public IP to see the website
Website is working all fine.
Now let's see, How auto scaling is functioning when this instance CPU utilization reaches more than 70%
Login to EC2 Instance and use stress command to add extra load on the instnace using stress; see as highlighted below instance has reach more than 70% CPU.
As per our configuration, It was set to launch a new instance if CPU utilization is more than 70%, Here, I have got notified via email that, a new instance launch is in progress!
Let us check in the AWS console we can see one more instance was created from the ASG with same configuration.
BOOM! Your Auto Scaling is working as expected!
Also, the newly created last instance got terminated by itself by the primary instance CPU utilization is below 70%!
This is how I have been notified!
That's it! The Auto Scaling group is now set up. It will automatically adjust the number of EC2 instances based on the policies you've defined. You can always modify the ASG, its policies, or the associated launch configuration later as needed.