Recently, we’re about to launch a new app but we did not have any idea about the traffic we’ll get. It could have been in hundreds or even thousands. So, we decided to go for Amazon’s Auto Scaling feature. Auto Scaling is the appropriate solution for your app when you are not certain about your app’s traffic or in cases where the traffic is really high or expected to go high during a certain period of time; be it for an hour, a day or a week.
Before we discuss on how to setup auto scaling, we will discuss a bit about what is auto scaling and how it can help you tackle those unknown and uncertain traffic scenarios.
What is auto scaling?
It is a service provided by Amazon Web Services which allows you to scale up or down your infrastructure horizontally by automatically adding or removing EC2 instances based on user-defined policies, health status checks or schedule.
Auto-scaling allows you to scale your computed resources dynamically or predictably:
- In Dynamic scaling you can start and stop ec2 instances based on different pre-defined conditions.
- In Predictable scaling you are certain about traffic patterns such as cases where traffic peaks every morning and goes down in the afternoon; you can have auto scaling start more Web servers in the morning and shutdown unnecessary ones in afternoon.
Following are the steps which can be followed to setup auto scaling:
Prerequisite for setting up an auto scaling
- AMI (Amazon Machine Image): In the beginning you set up only one instance in which you install all the required applications, necessary for your Website. After that, copy it to an AMI and use that image for launching more instances.But there are few drawbacks in this method which you need to keep in mind, such as: an image may become out of date during the period of time and the difficulty of keeping track of many types of images for different purposes.There are more flexible ways to tackle these, as you can fire up simple ec2 instance and configure it on the fly where time of booting for this one can use any of the configuration management tools like Puppet/Chef.
- Auto Scaling command line tool :In order to install the Auto Scaling Command line one can follow the instruction mentioned here: http://docs.aws.amazon.com/AutoScaling/latest/DeveloperGuide/UsingTheCommandLineTools.html
- Cloud Watch Monitoring Tool:You can define ‘cloud watch monitoring’ alert to trigger auto scaling policy. To install Cloud Watch CLI tools follow the instruction here: http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/SetupCLI.html
- Elastic Load Balancer: Whenever demand fluctuates, auto scaling dynamically adds and removes EC2 instances from the load balancer. So to insure that the traffic is distributed across all the running EC2 instances, you can use Load Balancer. It automatically distributes incoming Web traffic to multiple EC2 instances.
Configuring auto scaling:
It consists of four simple steps:
- Launch Configuration: - In launch config, you specify the template that auto scaling uses to launch EC2 instances. In the below mentioned command you define the launch config name, AMI, instance type and the security group for the EC2 instances.
as-create-launch-config ig-aslc --image-id ami-b69e37e4 --instance-type m1.small --group testingAS
- Auto Scaling Group : Auto Scaling Group is the core part of auto scaling service. Here, you can define settings like minimum, maximum and desired number of EC2 instances for an auto scaling group.
as-create-auto-scaling-group ig-asg --launch-configuration ig-aslc --availability-zones "ap-southeast-1a","ap-southeast-1b" --load-balancers igASelb --health-check-type ELB --grace-period 300 --max-size 6 --min-size 2 --desired-capacity 2
Now based on ig-asg group and ig-aslc, launch configuration auto scaling will launch two EC2 instances in two different availability zones: ap-southeast-1a and ap-southeast-1b respectively. And both the instances will be behind the igASelb Elastic Load balancer.
- Auto Scaling Policy: Auto scaling group takes action as per the auto scaling policy.
So now you will create two auto scaling policies:ig-scaling-out will increase the capacity of the ig-asg group and ig-scaling-in which decrease the capacity of ig-asg group.
1) Scaling Out Policy :
as-put-scaling-policy ig-scaling-out --auto-scaling-group ig-asg --adjustment=2 --type ChangeInCapacity --cooldown 300
When you execute the above command it will returns the ARN that serves as a unique name for new policy.
Now you’ll save this ARN at safe place as it’ll be required when you configure monitoring.2) Scaling Down Policy :
as-put-scaling-policy ig-scaling-in --auto-scaling-group ig-asg --adjustment=-2 --type ChangeInCapacity --cooldown 300
Above command will return the ARN, You will save this too.
Cool Down period in auto scaling policy takes care of scaling activities (instance launch or instance terminate).
As per mentioned command, you are using 300 seconds cool down period.
Let’s say our auto scaling up policy has launched two new instances so it won’t terminate the server immediately. It will wait for 300 sec and then terminate the instances.
- Monitoring Alarms :- Now add a Cloud Watch monitor so if CPU load is more than 70% for more than 5 minutes, it will execute scaling out policy and whenever load goes down it will execute scale down policy.
Below are the two commands which will do the same:
mon-put-metric-alarm --alarm-name sample-scale-up --alarm-description "Scale Up at 70%" --metric-name CPUUtilization --namespace AWS/EC2 --statistic Average --period 60 --threshold5 --comparison-operator GreaterThanThreshold --evaluation-periods 5 --unit Percent --alarm-actions arn:aws:autoscaling:us-east-1:xxxxxxxxxxxxxx:scalingPolicy:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx:autoScalingGroupName/ig-asg:policyName/ig-scale-out.
mon-put-metric-alarm --alarm-name sample-scale-dn --alarm-description "Scale down at55% load" --metric-name CPUUtilization --namespace AWS/EC2 --statistic Average --period60 --threshold 5 --comparison-operator LessThanThreshold --evaluation-periods 5 --unit Percent --alarm-actions arn:aws:autoscaling:us-east-1:xxxxxxxxxxxxxx:scalingPolicy:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx:autoScalingGroupName/ig-asg:policyName/ig-scale-in
That’s it! You can find more information about auto scaling here.