Skip to content

Instantly share code, notes, and snippets.

@innomatics
Last active March 20, 2024 10:32
Show Gist options
  • Save innomatics/b0838d9212f1b768a8ee98c27d2f8fb1 to your computer and use it in GitHub Desktop.
Save innomatics/b0838d9212f1b768a8ee98c27d2f8fb1 to your computer and use it in GitHub Desktop.
ECR Usage Report
#!/bin/bash
set -e
account_prefix=agrinous
accounts="master dev stg etc"
format_bytes() {
echo $1 | numfmt --to=iec-i
}
get_account_ecr_storage() {
total_bytes=0
integer_re='^[0-9]+$'
account=agrinous$1
for repo_name in $(aws ecr describe-repositories \
--profile $1 \
--query 'repositories[*].repositoryName' \
--output text)
do
repo_bytes=$(aws ecr describe-images \
--profile $1 \
--repository-name $repo_name \
--query 'imageDetails[*].imageSizeInBytes' | jq 'add')
if [[ $repo_bytes =~ $integer_re ]]; then
total_bytes=$((total_bytes + repo_bytes))
repo_bytes_fmt=$(format_bytes $repo_bytes)
else
repo_bytes=0
repo_bytes_fmt=""
fi
printf >&2 "%-30s %15s %6s\n" $repo_name $repo_bytes $repo_bytes_fmt
done
echo $total_bytes
}
org_bytes=0
for account_name in $accounts
do
printf "\n%s:\n" $account_name
account_bytes=$(get_account_ecr_storage $account_prefix$account_name)
org_bytes=$((org_bytes + account_bytes))
printf "\nAccount total: %s\n" $(format_bytes $account_bytes)
done
printf "\nOrg total: %s\n" $(format_bytes $org_bytes)
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment