Skip to content

Commit 551ff5e

Browse files
committed
feat: add lambda-prune script
1 parent f5ecae2 commit 551ff5e

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

.github/workflows/deploy.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,12 @@ jobs:
121121
with:
122122
aws_oidc_role_arn: ${{ env.AWS_OIDC_ROLE_ARN }}
123123
just_action: lambda-deploy
124+
125+
- name: prune lambda
126+
uses: chrispsheehan/just-aws-oidc-action@0.1.3
127+
env:
128+
FUNCTION_NAME: ${{ steps.get-api-vars.outputs.lambda_function_name }}
129+
ALIAS_NAME: ${{ steps.get-api-vars.outputs.lambda_alias_name }}
130+
with:
131+
aws_oidc_role_arn: ${{ env.AWS_OIDC_ROLE_ARN }}
132+
just_action: lambda-prune

justfile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,30 @@ lambda-deploy:
230230
echo "❌ Deployment $DEPLOYMENT_ID did not complete within expected time."
231231
exit 1
232232

233+
234+
lambda-prune:
235+
#!/usr/bin/env bash
236+
KEEP=3
237+
238+
live_version=$(aws lambda get-alias \
239+
--function-name "$FUNCTION_NAME" \
240+
--name "$ALIAS_NAME" \
241+
--region "$REGION" \
242+
| jq -r '.FunctionVersion')
243+
244+
echo "Alias '$ALIAS_NAME' points to: ${live_version:-<none>}"
245+
246+
# All published versions (exclude $LATEST), newest first
247+
versions=$(aws lambda list-versions-by-function \
248+
--function-name "$FUNCTION_NAME" \
249+
--region "$AWS_REGION" \
250+
| jq -r '.Versions[] | select(.Version != "$LATEST") | .Version' \
251+
| sort -nr)
252+
253+
keep_newest=$(echo "$versions" | head -n "$KEEP")
254+
keep_set=$(printf "%s\n%s\n" "$keep_newest" "$live_version" | sort -u)
255+
to_delete=$(comm -23 <(echo "$versions" | sort -u) <(echo "$keep_set" | sort -u))
256+
257+
echo "Keeping: $(echo "$keep_set" | tr '\n' ' ')"
258+
echo "Deleting: $(echo "$to_delete" | tr '\n' ' ')"
259+
# aws lambda delete-function --function-name "$FUNCTION_NAME" --qualifier "$v" --region "$REGION"

0 commit comments

Comments
 (0)