Skip to content

Instantly share code, notes, and snippets.

@acsrujan
Last active September 2, 2016 06:51
Show Gist options
  • Save acsrujan/062db33fb25721b67760 to your computer and use it in GitHub Desktop.
Save acsrujan/062db33fb25721b67760 to your computer and use it in GitHub Desktop.
Detach and Attach a random instance to an ELB.
#!/bin/bash
LOAD_BALANCER_NAME=$1
Instance_ids_str=`aws elb describe-load-balancers --load-balancer-name $LOAD_BALANCER_NAME --query "LoadBalancerDescriptions[*].{ID:LoadBalancerName,InstanceId:Instances[*].InstanceId}[*]. {ELB:ID,InstanceId:InstanceId[*]}" --output=json |jq -r ".[].InstanceId|@csv" |sed 's/\"//g'`
IFS=',' read -a Instance_ids_array <<< "$Instance_ids_str"
for INSTANCE_ID in ${Instance_ids_array[$RANDOM % ${#Instance_ids_array[@]}]}
do
aws elb deregister-instances-from-load-balancer --load-balancer-name $LOAD_BALANCER_NAME --instances $INSTANCE_ID
aws elb register-instances-with-load-balancer --load-balancer-name $LOAD_BALANCER_NAME --instances $INSTANCE_ID
done
@acsrujan
Copy link
Author

acsrujan commented Mar 28, 2016

To execute:

wget https://gist.githubusercontent.com/acsrujan/062db33fb25721b67760/raw/78492eade933963072f957cc344996fead5b48af/ELB-detach-attach.sh
#raw url of above gist
chmod +x ELB-detach-attach.sh
#run the script whenever necessary.

@vikas17a
Copy link

@acsrujan only one instance needs to be kicked out not all

@vikas17a
Copy link

got it

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