Skip to content

Instantly share code, notes, and snippets.

@seventhskye
Last active October 6, 2015 10:47
Show Gist options
  • Save seventhskye/691ba4657838c7da2de3 to your computer and use it in GitHub Desktop.
Save seventhskye/691ba4657838c7da2de3 to your computer and use it in GitHub Desktop.
A short script to register or deregister an instance with an Elastic Load-balancer. Will require IAM permissions and is assumed to be run from the instance.
#!/bin/bash
if [ $# -eq 0 ]; then
echo "Usage: $0 register|deregister <LOAD_BALANCER_NAME>"
echo "e.g. $0 register load-balancer-name"
else
INSTANCE_ID=$(wget -q -O- http://169.254.169.254/latest/meta-data/instance-id)
REGION=$(wget -q -O- http://169.254.169.254/latest/meta-data/placement/availability-zone | sed -e 's/\([1-9]\).$/\1/g')
if [ "$1" == "deregister" ]; then
aws elb $1-instances-from-load-balancer \
--load-balancer-name $2 \
--instances $INSTANCE_ID \
--region $REGION
elif [ "$1" == "register" ]; then
aws elb $1-instances-with-load-balancer \
--load-balancer-name $2 \
--instances $INSTANCE_ID \
--region $REGION
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment