Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.