How to automatically remove an instance from its LoadBalance (ELB) when you stop the instance?
I've tried to create a shutdown script and put it on chkconfig, but it is not working:
#!/bin/bash
# loggly authorization
#
# chkconfig: 2345 20 10
# description: add host logs to loggly
# Source function library.
. /etc/init.d/functions
. /etc/profile.d/aws-apitools-common.sh
LB=MyLB
OK="\033[60G\e[0;32mOK\e[0m"
EC2_INSTANCE_ID="`wget -q -O - http://169.254.169.254/latest/meta-data/instance-id || die \"wget instance-id has failed: $?\"`"
export JAVA_HOME=/usr/java/default
start() {
echo "started service"
}
stop() {
echo "removing $EC2_INSTANCE_ID from Load Balancer"
elb-deregister-instances-from-lb $LB --instances=$EC2_INSTANCE_ID -I $AWS_ACCESS_KEY_ID -S $AWS_SECRET_ACCESS_KEY --quiet
sleep 20
}
restart() {
stop
sleep 5
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
esac
exit $?
There must be a
/var/lock/subsys/my_app
filePut a
touch /var/lock/subsys/my_app
in the startup function and arm /var/lock/subsys/my_app
in the stop function