Skip to content

Instantly share code, notes, and snippets.

@xymor
Last active June 7, 2018 06:38
Show Gist options
  • Save xymor/8824186 to your computer and use it in GitHub Desktop.
Save xymor/8824186 to your computer and use it in GitHub Desktop.
script to automatically attach and detach an EC2::Instance from elbs
Our architecture uses an Ec2:Instance to define a master,
such instance doesn't have the automatic management of elb attachment
provided by the AWS::AutoScaling::AutoScalingGroup to slaves so
this script roughly replicates it by attaching an instance
upon boot and releasing it during shutdown.
#!/bin/bash
# chkconfig: 12345 99 01
# /etc/init.d/elb-controller
# description: Registra esta instância nos ELBs.
. /etc/init.d/functions
STORES=`cat /etc/available-elbs`
AWS_REGION=`cat /etc/aws-region`
export JAVA_HOME=/usr/lib/jvm/jre-1.6.0
export AWS_ELB_HOME=/opt/aws/apitools/elb
function start () {
for STORE in $STORES
do
/opt/aws/bin/elb-register-instances-with-lb $STORE --instances `wget -q -O - http://169.254.169.254/latest/meta-data/instance-id` --access-key-id XXX --secret-key YYY --region $AWS_REGION
done
touch /var/lock/subsys/elb-controller
}
function stop () {
for STORE in $STORES
do
/opt/aws/bin/elb-deregister-instances-from-lb $STORE -instances `wget -q -O - http://169.254.169.254/latest/meta-data/instance-id` --access-key-id XXX --secret-key YYY --region $AWS_REGION
done
rm /var/lock/subsys/elb-controller
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 2
start
;;
*)
echo "Uso: $0 {start|stop|restart}"
esac
chmod 755 /etc/init.d/elb-controller
chkconfig --add elb-controller
chkconfig elb-controller --level 12345 on
chkconfig elb-controller --level 06 off
echo "$AWS_REGION" >> /etc/aws-region
echo "$ELBS" >> /etc/available-elbs
@kothapallyn
Copy link

can you please post something similar for Windows instances as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment