Last active
June 7, 2018 06:38
-
-
Save xymor/8824186 to your computer and use it in GitHub Desktop.
script to automatically attach and detach an EC2::Instance from elbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can you please post something similar for Windows instances as well?