Skip to content

Instantly share code, notes, and snippets.

@kainam00
Created August 13, 2015 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kainam00/995ab825de02826e7f4c to your computer and use it in GitHub Desktop.
Save kainam00/995ab825de02826e7f4c to your computer and use it in GitHub Desktop.
Script to suspend and resume AWS autoscaling groups (ASG)
#!/bin/bash
TMPFILE="/tmp/asgs.temp"
function usage()
{
echo "Usage: $0 -a {suspend|resume} -f name-filter -p profile-name"
echo "Where: "
echo " -a - Action - suspend or resume autoscaling."
echo " -f - Name filter - filter for autoscaling groups. Allows you to select autoscaling groups matching a specific string."
echo " -p - Name of the AWS CLI profile. (i.e. what you would pass to the --profile in an aws cli command)"
exit 1
}
if [ "$#" -lt 5 ] ; then
usage
fi
while getopts "a:f:p:" opt; do
case $opt in
a)
ACTION=$OPTARG
;;
f)
NAMEFILTER=$OPTARG
;;
p)
PROFILE=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG"
usage
;;
esac
done
# Cleanup
rm -fv $TMPFILE
# Find ASGs
for asg in $(aws autoscaling describe-auto-scaling-groups --profile $PROFILE --query 'AutoScalingGroups[*].AutoScalingGroupName' --output text) ; do echo $asg ; done | grep $NAMEFILTER > $TMPFILE
# Suspend ASG
echo "About to $ACTION the following ASGs: "
cat $TMPFILE
echo "Press control-C in the next 5 seconds to abort"
sleep 6
for asg in $(cat $TMPFILE) ; do aws autoscaling ${ACTION}-processes --auto-scaling-group-name $asg --profile $PROFILE ; done
echo "Done. The AWS CLI commands don't generate any output, so if there are no errors above you should be good."
# Cleanup
echo "Cleaning up..."
rm -fv $TMPFILE
@kainam00
Copy link
Author

This script allows you to quickly/easily suspend and resume AWS autoscaling groups matching a specific string. Requires AWS CLI tools installed and profiles set up.

@vikten22
Copy link

Hi,

I wanted to run this script on my aws linux instances, aws profile is already setup.
Can you help me what else do i need to setup to make this working?

Thanks

@kainam00
Copy link
Author

It's been ages since I've used it, but if you have a working profile that you know works with AWS cli tools, it should be as simple as:

./asg-control.bash -a suspend -f -p

Note that you need to have aws cli tools installed and working for this script to work.

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