Skip to content

Instantly share code, notes, and snippets.

@dev1x
Last active December 11, 2017 07:54
Show Gist options
  • Save dev1x/a35357ee17d32f4d5297c6bf93edaa0e to your computer and use it in GitHub Desktop.
Save dev1x/a35357ee17d32f4d5297c6bf93edaa0e to your computer and use it in GitHub Desktop.
Elastic Beanstalk AppVersions clean
#!/bin/bash
#
# TODO: allow for region to be added
# set EB Application Name and Limit
ebAppName=${@:2}
ebAppLimit=${1:-"5"}
if [ "$#" -lt 1 ]; then
echo "This script requires at least one argument for the number of versions to keep on beanstalk"
echo "Usage: eb-clean <number_of_versions_to_keep> <multiple_applications_name>"
echo "Default: 5 versions of all the applications"
echo "ATTENTION: If no application is added this will perform a delete on ALL the applications on that region"
exit 1
fi
if [ "$#" -lt 2 ]; then
ebAppName=$(aws elasticbeanstalk describe-applications --output=text | grep APPLICATIONS | awk '{print $2}')
fi
# tail +$var is inclusive, so we need to increment 1 to exclude the last value of the ebAppLimit we set
tailebAppLimit=$((ebAppLimit+1))
for ebAppName in $ebAppName
do
echo "------------------------------------"
echo "$(date) checking app:$ebAppName with ebAppLimit:$ebAppLimit"
# get app versions above the ebAppLimit as a list
versions=$(aws elasticbeanstalk describe-application-versions --application-name $ebAppName --output text | grep APPLICATIONVERSIONS | tail -n +$tailebAppLimit| awk '{print $6}' | awk 'NF > 0')
# delete obsolete versions
for version in $versions
do
aws elasticbeanstalk delete-application-version --delete-source-bundle --version-label "$version" --application-name "$ebAppName"
echo "$(date) $ebAppName:$version deleted"
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment