Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Quick and dirty script to check the number of images in ECR repositories and if there is an associated lifecycle policy
#!/bin/bash
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
REPOS=$(aws ecr describe-repositories --query 'repositories[*].repositoryName' | jq -r '.[]')
for r in $REPOS ; do
printf $r
COLOR=$GREEN
IMAGES=$(aws ecr describe-images --repository-name $r --query 'imageDetails[*].imageDigest' | jq -r '.[]')
IMAGES_COUNT=$(echo $IMAGES | wc -w)
if [ $IMAGES_COUNT -gt 500 ] ; then
COLOR=$RED
fi
printf " ${COLOR}$IMAGES_COUNT ${NC}"
COLOR=$GREEN
POLICY='policy'
$(aws ecr get-lifecycle-policy --repository-name $r > /dev/null 2>&1)
if [ $? != 0 ] ; then
COLOR=$RED
POLICY='no_policy'
fi
printf "${COLOR}$POLICY${NC}\n"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment