-
-
Save nbr23/a5ba05fd6c95bdfd83b0273c4217d607 to your computer and use it in GitHub Desktop.
AWS: Calculate the size of all ECR repositories
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| repos="" | |
| sizes="" | |
| name_lens="" | |
| image_counts="" | |
| # Check if user is logged in | |
| if ! aws sts get-caller-identity &> /dev/null; then | |
| echo "ERROR: Seems like your SSO session is invalid. Please run" | |
| printf "\n $ aws sso login\n\n" | |
| echo "before you run the script." | |
| exit 1 | |
| fi | |
| data=$(aws ecr describe-repositories --output json | jq .repositories) | |
| repo_count=$(echo $data | jq length) | |
| index=1 | |
| for name in $(echo $data | jq -r .[].repositoryName); do | |
| # Progress | |
| echo -en "\033[K" | |
| echo -n "[$index/$repo_count] GET $name" $'\r' | |
| # Get repo images | |
| repo_images=$(aws ecr describe-images --repository-name "$name" --output json) | |
| # Get size | |
| size=$(echo $repo_images | jq .imageDetails[].imageSizeInBytes | awk '{s+=$1}END{OFMT="%.0f";print s}') | |
| # Get image count | |
| image_count=$(echo $repo_images | jq '.imageDetails | length') | |
| if [ -n "$size" ]; then | |
| raw_size="$size" | |
| size=$(numfmt --to=iec --suffix=B --format "%.2f" $size) | |
| else | |
| raw_size="0" | |
| size="<no-images>" | |
| fi | |
| repos="${repos}$name $size $image_count\n" | |
| sizes="${sizes}$raw_size\n" | |
| name_lens="${name_lens}${#name}\n" | |
| image_counts="${image_counts}$image_count\n" | |
| index=$(expr $index + 1) | |
| done | |
| # Sort repos by size | |
| repos=$(printf "$repos" | sort -k2 -h) | |
| # Add separator before total | |
| max_name_len=$(printf $name_lens | sort -n | tail -1) | |
| repos="${repos}\n$(printf -- '-%.0s' $(seq ${max_name_len})) -------- --------\n" | |
| # Add total size and image count | |
| total_size=$(printf $sizes | awk '{s+=$1}END{OFMT="%.0f";print s}') | |
| total_images=$(printf $image_counts | awk '{s+=$1}END{print s}') | |
| repos="${repos}TOTAL $(numfmt --to=iec --suffix=B --format "%.2f" $total_size) $total_images\n" | |
| # Print final table | |
| printf "REPOSITORY SIZE IMAGE_COUNT\n$repos" | awk ' | |
| BEGIN { | |
| FS=" "; | |
| OFS="\t"; | |
| } | |
| { | |
| if (NR == 1) { | |
| header1 = $1; | |
| header2 = $2; | |
| header3 = $3; | |
| } else { | |
| $2 = sprintf("%10s", $2); | |
| $3 = sprintf("%11s", $3); | |
| } | |
| print $0; | |
| }' | column -t |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great script to quickly have a view on ECR status!