Skip to content

Instantly share code, notes, and snippets.

@jcttrll
Last active July 12, 2017 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcttrll/c7b8cdb8eca938aeb86094c34ab1afb8 to your computer and use it in GitHub Desktop.
Save jcttrll/c7b8cdb8eca938aeb86094c34ab1afb8 to your computer and use it in GitHub Desktop.
Usage: lambda-storage-calc.sh --region <aws-region>
#!/bin/bash -e
if [ $# -ne 2 -o ! "$1" = "--region" ]; then
echo "Usage: ${0##*/} --region <aws-region>" 2>&1
exit 1
fi
AWS_REGION="$2"
AWS_OPTIONS+=" --region $AWS_REGION --output json"
lambdas=($(aws $AWS_OPTIONS lambda list-functions | jq -r '.Functions[].FunctionName'))
for lambda in "${lambdas[@]}"; do
aws $AWS_OPTIONS lambda list-versions-by-function --function-name "$lambda" |
jq -r '.Versions[] | "\(.LastModified)\t\(.Version)\t\(.CodeSize)\t\(.Description)"' | sort |
awk -v lambda="$lambda" '
{
date = $1;
version = $2;
size = $3;
sub("[^\t]+\t[^\t]+\t[^\t]+\t", "");
description = $0;
count++;
overallSize += size;
if (version == "$LATEST") {
latest = size;
}
printf("\t%s %7s %10d bytes: %s\n", date, version, size, description);
}
END {
if (count > 1) {
overallSize -= latest;
}
v = count == 1 ? "version" : "versions";
printf("%s - %d %s, %d total bytes\n", lambda, count, v, overallSize);
}
' | tac
done
@jcttrll
Copy link
Author

jcttrll commented Jul 12, 2017

Note that, if necessary, you can pass options to the AWS CLI commands by setting AWS_OPTIONS outside the script, like:

export AWS_OPTIONS='--profile foo --debug'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment