Skip to content

Instantly share code, notes, and snippets.

@rcbop
Created June 15, 2017 21:09
Show Gist options
  • Save rcbop/1096858220c382997a00217f02f554e1 to your computer and use it in GitHub Desktop.
Save rcbop/1096858220c382997a00217f02f554e1 to your computer and use it in GitHub Desktop.
Delete AWS elastic beanstalk versions older than given arbitrary number
#!/bin/bash
# mantainer rogerio.c.peixoto (rcbpeixoto@gmail.com)
# delete old application versions preserving most recent ones
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
MAGENTA='\033[35m'
NC='\033[0m'
AWSARGS="--region us-east-1"
[ -z "$1" ] && VERSIONS_TO_KEEP=5 || VERSIONS_TO_KEEP=$1
APP_LIST=$(aws elasticbeanstalk describe-applications --output json $AWSARGS | jq -r '.Applications[].ApplicationName')
for app in $APP_LIST
do
COUNTER=0
echo -e "$CYAN[$app]$NC"
VERSION_LIST=$(aws elasticbeanstalk describe-application-versions --output json $AWSARGS | jq -r --arg app $app '.ApplicationVersions[] | select(.ApplicationName==$app) | .VersionLabel')
for version in $VERSION_LIST
do
((COUNTER++))
if [ $COUNTER -gt $VERSIONS_TO_KEEP ]; then
echo -e " $RED> deleting $YELLOW$version $NC"
aws elasticbeanstalk delete-application-version $awsargs --application-name $app --version-label $version
else
echo -e " $GREEN> keeping $version $NC"
fi
done
echo -e "$MAGENTA>> total versions $COUNTER $NC"
done
[ `uname` == "Darwin" ] && say -v "Alex" "Job Complete"
echo -e "$GREEN##### TASK COMPLETE #####$NC"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment