Last active
November 15, 2024 13:37
-
-
Save RafaelWO/8917d6b8618fa825304b706ea65ccbee to your computer and use it in GitHub Desktop.
AWS: Calculate the size of all ECR repositories
This file contains 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="" | |
# 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 size | |
size=$(aws ecr describe-images --repository-name "$name" --output json | jq .imageDetails[].imageSizeInBytes | awk '{s+=$1}END{OFMT="%.0f";print s}') | |
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\n" | |
sizes="${sizes}$raw_size\n" | |
name_lens="${name_lens}${#name}\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 | |
total=$(printf $sizes | awk '{s+=$1}END{OFMT="%.0f";print s}') | |
repos="${repos}TOTAL $(numfmt --to=iec --suffix=B --format "%.2f" $total)\n" | |
# Print final table | |
printf "$repos" | column -t --table-columns REPOSITORY,SIZE -R SIZE |
Thanks for the kind words! 🙏
weird I had to use the
-c
option instead of the--table-columns
one
I also had to do the same tweak
Interesting, what shell did you use? The man page that I checked lists --table-columns
.
Thanks for the kind words! 🙏
weird I had to use the
-c
option instead of the--table-columns
oneI also had to do the same tweak
Interesting, what shell did you use? The man page that I checked lists
--table-columns
.
Mostly like a BSD vs GNU problem.
COLUMN(1) General Commands Manual COLUMN(1)
NAME
column – columnate lists
SYNOPSIS
column [-tx] [-c columns] [-s sep] [file ...]
DESCRIPTION
The column utility formats its input into multiple columns. Rows are filled before columns. Input is taken from file operands, or, by default, from
the standard input. Empty lines are ignored.
The options are as follows:
-c Output is formatted for a display columns wide.
-s Specify a set of characters to be used to delimit columns for the -t option.
-t Determine the number of columns the input contains and create a table. Columns are delimited with whitespace, by default, or with the
characters supplied using the -s option. Useful for pretty-printing displays.
-x Fill columns before filling rows.
ENVIRONMENT
The COLUMNS, LANG, LC_ALL and LC_CTYPE environment variables affect the execution of column as described in environ(7).
EXIT STATUS
The column utility exits 0 on success, and >0 if an error occurs.
EXAMPLES
(printf "PERM LINKS OWNER GROUP SIZE MONTH DAY " ; \
printf "HH:MM/YEAR NAME\n" ; \
ls -l | sed 1d) | column -t
SEE ALSO
colrm(1), ls(1), paste(1), sort(1)
HISTORY
The column command appeared in 4.3BSD-Reno.
BUGS
Input lines are limited to LINE_MAX (2048) bytes in length.
macOS 14.3 July 29, 2004 macOS 14.3
If you are on macos, you will also need to use gnumfmt
instead of numfmt
.
Awesome script, I also had to use printf "$repos" | column -t -c REPOSITORY,SIZE
for line #50
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice script 🙌 , weird I had to use the
-c
option instead of the--table-columns
one (which is not in the man pages), so for me the line #41 works as:printf "$repos" | column -t -c REPOSITORY,SIZE