Created
November 27, 2015 18:31
-
-
Save burdandrei/800748739ca4fed62fbf to your computer and use it in GitHub Desktop.
Sometimes you want to double the number of instances they to receive the new version, and then Autoscaling to hande the magic
This file contains hidden or 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
#!/usr/bin/env bash | |
# | |
# Double the instance count for widget ASG | |
# Required ENV Variables | |
# * ELB_NAME | |
# * ASG_NAME | |
# | |
asg_check() { | |
local pending_instance_count=$(aws autoscaling describe-auto-scaling-groups \ | |
--query 'length(AutoScalingGroups[].Instances[?LifecycleState==`Pending`].InstanceId[])' \ | |
--auto-scaling-group-names ${ASG_NAME}) | |
if [[ $pending_instance_count -ne "0" ]]; then | |
return 3 | |
else | |
return 0 | |
fi | |
} | |
elb_check() { | |
local outofservice_instance_count=$(aws elb describe-instance-health \ | |
--load-balancer-name ${ELB_NAME} \ | |
--query 'length(InstanceStates[?State==`OutOfService`].InstanceId)') | |
if [[ $outofservice_instance_count -ne "0" ]]; then | |
return 3 | |
else | |
return 0 | |
fi | |
} | |
wait_for() { | |
local check_function=$1 | |
while ! $(eval $check_function) ; do | |
sleep 15 | |
echo "." | |
done | |
} | |
check_for_instances_in_service() { | |
INSTANCES_IN_SERVICE=$(aws elb describe-instance-health \ | |
--load-balancer-name ${ELB_NAME} \ | |
--query 'length(InstanceStates[?State==`InService`].InstanceId)') | |
} | |
echo "Checking number of InService instances for ElasticLoadBalancer™" | |
check_for_instances_in_service | |
echo "Ok, We got ${INSTANCES_IN_SERVICE} InService instances running" | |
DESIRED_INSTANCES=$(expr $INSTANCES_IN_SERVICE \* 2) | |
echo "Let's make it ${DESIRED_INSTANCES}" | |
aws autoscaling set-desired-capacity --auto-scaling-group-name ${ASG_NAME} --desired-capacity ${DESIRED_INSTANCES} --no-honor-cooldown | |
echo "Wait for Autoscale to Pend the machines" | |
sleep 30 | |
echo "Waiting for Autoscale to add Instances" | |
wait_for asg_check | |
echo "Waiting for Instances to be InService in ElasticLoadBalancer™" | |
wait_for elb_check | |
check_for_instances_in_service | |
echo "Well, I'm done" | |
echo "Now we got ${INSTANCES_IN_SERVICE} InService instances running" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment