Created
March 29, 2023 21:17
-
-
Save johnnybenson/1d00c1ba7ffb0c9b7df3cb3269251654 to your computer and use it in GitHub Desktop.
Clean up old AWS Lambda Functions
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requires jq