by Andrii Kozhokaru
I've recently stumbled upon a small yet unpleasant scaling-related issue - when I stop a host it keeps on accepting requests for about 30-35 seconds. Naturally these requests can't be processed. That is why an idea crossed my mind to cut instances from load balancer before stopping a web server.
For this purpose we need Command Line Tools for ELB.
Let's unpack them to the folder /opt/aws/elb.
Place the pk-** and cert-** certificates to /opt/aws/keys.
The JAVA SDF is /usr/java/latest.
The actual script is as follows:
INSTANCE_ID=`wget -q -O - http://169.254.169.254/latest/meta-data/instance-id`
elb-deregister-instances-from-lb $1 --instances $INSTANCE_ID
Next, add the following line to the beginning of the stop function in the Apache/Tomcat/Nginx (or any other) initialization script:
sh /opt/aws/kick_from_lb.sh $elb_name
- $elb_name - the name of your Elastic Load Balancer.
Done. Now before the web-server stops the instance will be automatically cut from the balancer and will not accept requests.
P.S. Do not forget to introduce all the changes to the AMI.