Created
January 16, 2015 04:05
-
-
Save joehack3r/faae9bb6b2c21a3491be to your computer and use it in GitHub Desktop.
Terminate instance and decrement capacity in ASG
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 | |
if [ -n "$1" ]; then | |
minutesBeforeTerminate=$1 | |
else | |
minutesBeforeTerminate=1440 | |
fi | |
decimal=0 | |
secondsUptime=`awk {'print $1'} /proc/uptime` | |
minutesUptime=`echo "scale=$decimal; $secondsUptime / 60" | bc` | |
loggedInUsers=`users | wc -l` | |
function scaleDown | |
{ | |
instanceId=`curl -s -l http://169.254.169.254/latest/meta-data/instance-id` | |
az=`curl -s -l http://169.254.169.254/latest/meta-data/placement/availability-zone` | |
region=${az%?} | |
aws autoscaling terminate-instance-in-auto-scaling-group --instance-id $instanceId --should-decrement-desired-capacity --region $region | |
} | |
if [ $loggedInUsers -eq 0 ] && [ $minutesUptime -ge $minutesBeforeTerminate ]; then | |
echo "Number of users logged in:" $loggedInUsers | |
echo "Uptime in minutes:" $minutesUptime | |
echo "Self-terminating instance in ASG and decrementing capacity" | |
scaleDown | |
else | |
echo "Number of users logged in:" $loggedInUsers | |
echo "Uptime in minutes:" $minutesUptime | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment