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: http://aws.amazon.com/cli/
(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