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
@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