How to Load Balance and Auto Scale with Amazon’s EC2

by barce on August 2, 2009

This blog post is a quick introduction to load balancing and auto scaling on with Amazon’s EC2.

I was kinda amazed about how easy it was.

Prelims: Download the load balancer API software, auto scaling software, and cloud watch software. You can get all three at a download page on Amazon.

Let’s load balancer two servers.

elb-create-lb lb-example --headers \
--listener "lb-port=80,instance-port=80,protocol=http" \
--availability-zones us-east-1a

The above creates a load balancer called “lb-example,” and will load balance traffic on port 80, i.e. the web pages that you serve.

To attach specific servers to the load balancer you just type:

elb-register-instances-with-lb lb-example --headers \
--instances i-example,i-example2

where i-example and i-example2 are the instance id’s of the servers you want added to the load balancer.

You’ll also want to monitor the health of the load balanced servers, so please add a health check:

elb-configure-healthcheck lb-example --headers \
--target "HTTP:80/index.html" --interval 30 --timeout 3 \
--unhealthy-threshold 2 --healthy-threshold 2

Now let’s set up autoscaling:

as-create-launch-config example3autoscale --image-id ami-mydefaultami \
--instance-type m1.small
as-create-auto-scaling-group example3autoscalegroup  \
--launch-configuration example3autoscale \
--availability-zones us-east-1a \
--min-size 2 --max-size 20 \
--load-balancers lb-example
as-create-or-update-trigger example3trigger \
--auto-scaling-group example3autoscalegroup --namespace "AWS/EC2" \
--measure CPUUtlization --statistic Average \
--dimensions "AutoScalingGroupName=example3autoscalegroup" \
--period 60 --lower-threshold 20 --upper-threshold 40 \
--lower-breach-increment=-1 --upper-breach-increment 1 \
--breach-duration 120

With the 3 commands above I’ve created an auto-scaling scenario where a new server is spawned and added to the load balancer every two minutes if the CPU Utilization is above 20% for more than 1 minute.

Ideally you want to set –lower-threshold to something high like 70 and –upper-threshold to 90, but I set both to 20 and 40 respectively just to be able to test.

I tested using siege.

Caveats: the auto-termination part is buggy, or simply didn’t work. As the load went down, the number of the server on-line remained the same. Anybody have thoughts on this?

What does auto-scaling and load balancing in the cloud mean? Well, the total cost of ownership for scalable, enterprise infrastructure just went down by lots. It also means that IT departments can just hire a cloud expert and deploy solutions from a single laptop instead of having to figure out the cost for hardware load balancers and physical servers.

The age of Just-In-Time IT just got ushered in with auto-scaling and load balancing in the cloud.

{ 9 comments… read them below or add one }

Amila January 9, 2010 at 8:39 am

as-create-launch-config example3autoscale –image-id ami-mydefaultami \
–instance-type m1.small

I tried to create a launch config with a user data file as well
i.e –user-data-file

seems to be it is also not working. I get following error.

as-create-launch-config: program error

barce January 24, 2010 at 7:45 pm

You need to download the as tools. Also, apologies for not using double hyphens. e.g. this blog is messing things up a bit.


as-create-launch-config example3autoscale -–image-id ami-mydefaultami \
-–instance-type m1.small

Giles Smith November 5, 2010 at 5:24 am

Thanks for the article, was a great help in getting my scaling group up and running.

just one thing, you have a spelling mistake: CPUUtlization should be CPUUtilization

mina September 24, 2011 at 9:42 am

hi..
i configure all the AWS_AUTO_SCALING parameters.. and as-cmd works properly. but when i try to do as-create-launch-config i recieve program error,,, although i’ve done the exact same process on another instance and it works.. any help is really appreciated !! thanks :)

mina September 24, 2011 at 9:42 am

is it any thing with availability zone?

Nikolai November 18, 2011 at 7:21 am

Thank you for that very helpful guide. Couldn’t find the information on how to add an auto-scaled instance to a load balancer on aws website.

Elango February 12, 2012 at 6:07 pm

Hi
Thanks for the Guide. I have a query in the example we have created instacnce bases on cpu utilization , but if we want to do the same based on some custom logic , how can we achieve that ..

Elango

Athipathy October 7, 2013 at 3:19 am

Hi,
Is it possible to Scale a single VM vertically in amazon with the autoscaling feature ?

Note : the Maximum and Minimum size of the group will always remain 1 in my use case.
I just want to Scale up the Instance when a desired threshold reaches.

barce October 7, 2013 at 1:28 pm

Out of the box auto-scaling does not scale vertically.

You would have to write code that does the following:
1. Create a launch config with the next VM up in capacity and be sure that the current VM is using a static IP address.
2. When a threshold is reached, autoscaling will start a new instance that is “one up” in capacity.
3. Have code that de-commissions the smaller instance.
4. Have code that re-assigns the IP address of the smaller VM to the new larger one.
5. repeat step 1.

This is a general way to do it. If you have EBS volumes, you’ll have to write code to handle that. You’ll have to write code to handle any particular details.

I hope this helps.

Leave a Comment

{ 2 trackbacks }

Previous post:

Next post: