Skip to content

Commit 2c8e2c1

Browse files
feat: prune lambda versions (#5)
* feat: add lambda-prune script * fix: depends_on = [aws_lambda_alias.live] * feat: delete older x versions * chore: ci input lambda_keep
1 parent f5ecae2 commit 2c8e2c1

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

.github/workflows/deploy.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ on:
1717
description: "Valid lambda version"
1818
required: true
1919
type: string
20+
lambda_keep:
21+
description: "Number of lambda versions to keep"
22+
default: '5'
23+
type: string
2024

2125

2226
concurrency: # only run one instance of workflow at any one time
@@ -121,3 +125,13 @@ jobs:
121125
with:
122126
aws_oidc_role_arn: ${{ env.AWS_OIDC_ROLE_ARN }}
123127
just_action: lambda-deploy
128+
129+
- name: prune lambda
130+
uses: chrispsheehan/just-aws-oidc-action@0.1.3
131+
env:
132+
KEEP: ${{ inputs.lambda_keep }}
133+
FUNCTION_NAME: ${{ steps.get-api-vars.outputs.lambda_function_name }}
134+
ALIAS_NAME: ${{ steps.get-api-vars.outputs.lambda_alias_name }}
135+
with:
136+
aws_oidc_role_arn: ${{ env.AWS_OIDC_ROLE_ARN }}
137+
just_action: lambda-prune

infra/modules/aws/lambda/main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ resource "aws_appautoscaling_target" "pc_target" {
116116
resource_id = "function:${local.lambda_name}:${var.environment}"
117117
scalable_dimension = "lambda:function:ProvisionedConcurrency"
118118
service_namespace = "lambda"
119+
120+
depends_on = [aws_lambda_alias.live]
119121
}
120122

121123
resource "aws_appautoscaling_policy" "pc_policy" {

justfile

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,32 @@ 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+
live_version=$(aws lambda get-alias \
237+
--function-name "$FUNCTION_NAME" \
238+
--name "$ALIAS_NAME" \
239+
--region "$AWS_REGION" \
240+
| jq -r '.FunctionVersion')
241+
242+
echo "Alias '$ALIAS_NAME' points to: ${live_version:-<none>}"
243+
versions=$(aws lambda list-versions-by-function \
244+
--function-name "$FUNCTION_NAME" \
245+
--region "$AWS_REGION" \
246+
| jq -r '.Versions[] | select(.Version != "$LATEST") | .Version' \
247+
| sort -nr)
248+
249+
keep_newest=$(echo "$versions" | head -n "$KEEP")
250+
keep_set=$(printf "%s\n%s\n" "$keep_newest" "$live_version" | sort -u)
251+
to_delete=$(comm -23 <(echo "$versions" | sort -u) <(echo "$keep_set" | sort -u))
252+
253+
echo "Keeping version(s): $(echo "$keep_set" | tr '\n' ' ')"
254+
if [[ -z "${to_delete// }" ]]; then
255+
echo "Nothing to delete."
256+
exit 0
257+
fi
258+
for v in $to_delete; do
259+
echo "Deleting $FUNCTION_NAME:$v"
260+
aws lambda delete-function --function-name "$FUNCTION_NAME" --qualifier "$v" --region "$REGION"
261+
done

0 commit comments

Comments
 (0)