Skip to content

Instantly share code, notes, and snippets.

@StefanieD
Created August 11, 2015 17:31
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 StefanieD/64c3b07a70b3b350aea1 to your computer and use it in GitHub Desktop.
Save StefanieD/64c3b07a70b3b350aea1 to your computer and use it in GitHub Desktop.
Script for clean up app versions in AWS Elastic Beanstalk

This code snippet removes old app versions in AWS Elastic Beanstalk. Only a specific amount of app versions should stay in the bucket.

In this example only the last 6 versions for prod environment and 3 for staging will be kept.

files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/600_delete_old_versions.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
. /opt/elasticbeanstalk/support/envvars
if [ "$SYMFONY_ENV" = "prod" ]; then
LINECOUNT=$(aws elasticbeanstalk describe-application-versions --region 'eu-west-1' --output text --query 'ApplicationVersions[*].[ApplicationName,VersionLabel,DateCreated]' | grep "production" | wc -l)
if [[ $LINECOUNT -gt 6 ]]; then
aws elasticbeanstalk describe-application-versions --region 'eu-west-1' --output text --query 'ApplicationVersions[*].[ApplicationName,VersionLabel,DateCreated]' | grep "production" | tail -1 | while read app ver date; do aws elasticbeanstalk delete-application-version --region 'eu-west-1' --application-name $app --version-label $ver --delete-source-bundle; done
fi
fi
if [ "$SYMFONY_ENV" = "staging" ]; then
LINECOUNT=$(aws elasticbeanstalk describe-application-versions --region 'eu-west-1' --output text --query 'ApplicationVersions[*].[ApplicationName,VersionLabel,DateCreated]' | grep "staging" | wc -l)
if [[ $LINECOUNT -gt 3 ]]; then
aws elasticbeanstalk describe-application-versions --region 'eu-west-1' --output text --query 'ApplicationVersions[*].[ApplicationName,VersionLabel,DateCreated]' | grep "staging" | tail -1 | while read app ver date; do aws elasticbeanstalk delete-application-version --region 'eu-west-1' --application-name $app --version-label $ver --delete-source-bundle; done
fi
fi
echo "$(date +'%Y%m%d %T') deleted old versions" >> /var/log/directory-hooks-executor.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment