Skip to content

Instantly share code, notes, and snippets.

@johnnybenson
Created March 29, 2023 21:17
Show Gist options
  • Save johnnybenson/1d00c1ba7ffb0c9b7df3cb3269251654 to your computer and use it in GitHub Desktop.
Save johnnybenson/1d00c1ba7ffb0c9b7df3cb3269251654 to your computer and use it in GitHub Desktop.
Clean up old AWS Lambda Functions
function run() {
LAMBDA_FUNCTION=$1;
LAMBDA_VERSIONS_DATA=$(aws lambda list-versions-by-function --function-name $LAMBDA_FUNCTION);
LAMBDA_VERSIONS=$(echo $LAMBDA_VERSIONS_DATA | jq -r '.Versions | map(select(.Version != "$LATEST")) | map(.Version | tonumber)');
LAMBDA_VERSION_LATEST=$(echo $LAMBDA_VERSIONS | jq -r 'sort | .[-1:][0]');
echo "Latest Version: $LAMBDA_VERSION_LATEST";
LAMBDA_VERSIONS_ARR=($(echo $LAMBDA_VERSIONS | jq -r 'join(" ")'));
for version in "${LAMBDA_VERSIONS_ARR[@]}"; do
if (( version < LAMBDA_VERSION_LATEST )); then
echo "Removing old Version: $version";
echo "aws lambda delete-function --function-name $LAMBDA_FUNCTION:$version";
aws lambda delete-function --function-name $LAMBDA_FUNCTION:$version;
fi
done
}
run $1
@johnnybenson
Copy link
Author

> chmod +x ./clean_up_lambda.sh 
> ./clean_up_lambda.sh my_func

Requires jq

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