Automatically remove instance from ELB on server stop

by Daniel Cukier   Last Updated October 10, 2019 05:00 AM

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 $?


Answers 1


There must be a /var/lock/subsys/my_app file

Put a touch /var/lock/subsys/my_app in the startup function and a rm /var/lock/subsys/my_app in the stop function

Daniel Cukier
Daniel Cukier
January 22, 2013 19:36 PM

Related Questions


Updated December 30, 2018 01:00 AM

Updated December 14, 2017 22:00 PM

Updated March 24, 2017 09:00 AM

Updated November 06, 2017 07:00 AM