-
-
Save gregtaylor99/8ea0fb95a7101134afee07145bdd2324 to your computer and use it in GitHub Desktop.
deregister EC2 instanceId from all 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
#!/bin/bash | |
instance_id=$(curl -s http://169.254.169.254/latest/meta-data/instance-id) | |
az=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone) | |
region=${az%%?} | |
list_elb(){ | |
aws --region $region elb describe-load-balancers --query \ | |
"LoadBalancerDescriptions[?Instances[?InstanceId=='${1}']].LoadBalancerName" | |
} | |
dereg_from_elb() { | |
echo "deregister ${1} from ${2}" | |
aws --region $region elb deregister-instances-from-load-balancer --instances "$1" --load-balancer-name "$2" > /dev/null | |
} | |
do_dereg_from_elb() { | |
list_elb ${instance_id} | sed -ne 's/"\(.*\)".*/\1/p' | while read elb | |
do | |
dereg_from_elb $instance_id $elb | |
done | |
} | |
do_dereg_from_elb | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment