Amazon ELB and AutoScaling scripts



Amazon itself doesn’t provide Auto Scaling scripts, so you have to write your own ones. It’s up to you to decide when to start an instance and add it to load balancer. We can achieve this with API calls and for this purpose Amazon even provided a CLI. Link to CLI tools:

(Note: For some time now, AWS provides you with autoscaling. But there was none at the time, when we were designing our infrastructure.)


Recently I noticed Amazon’s Elastic Loadbalancer just isn’t working as it used to. Our servers were up and running, but loadbalancer just wouldn’t pick them up. Autoscaling script was starting new instances, to fit the needs, starting more instances than it was necessary. After an hour and around 4-5 additional instances started, ELB finally picked them up and our autoscaler script could stop the unneeded ones.


I found out that if you have already predefined pool of servers in loadbalancer and some of the instances are in stopped state, loadbalancer will take really long to refresh it’s knowledge about possible instance state changes. What I also noticed, removing and re-adding instance to the pool from web interface instantly fixes problem. So basically, when you are starting up an instance, you should wait a little (so that your server comes up and is running) and then re-add it to balancer. Unfortunately only re-adding doesn’t make loadbalancer re-check instance’s status, so you first have to de-register this instance and register it again, once it’s up. As I was checking Amazon’s knowledgebase I noticed that this is preferred way of letting loadbalancer know about the new state of instance.

So basically it comes up to 3 commands you have to do when starting instance.

ec2start --region YOUR_REGION INSTANCE_ID
aws elb deregister-instances-from-load-balancer --region YOUR_REGION --load-balancer-name YOUR_LOADBALANCER --instances INSTANCE_ID
aws elb register-instances-with-load-balancer --region YOUR_REGION  --load-balancer-name YOUR_LOADBALANCER --instances INSTANCE_ID

Leave a Reply

Purpose of the commenting system is to share your experience. I encourage you to post feedback with your own suggestions, ideas or optimizations regarding the topic of a blog post. What commenting system isn't for, is asking questions about similar issues of yours and requesting support for it. Blog post is provided as is and I am not here to solve all your problems. Please bear that in mind and try to avoid posting such comments. I do take privilege to remove comment from my blog for any reason whatsoever. Usually I do it when I sense a comment was posted only for spam/seo reasons or is out of blog post's topic. Thank you for reading this, now you may continue :)

Your email address will not be published. Required fields are marked *