File tree Expand file tree Collapse file tree 3 files changed +45
-0
lines changed
Expand file tree Collapse file tree 3 files changed +45
-0
lines changed Original file line number Diff line number Diff line change 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
2226concurrency : # 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
Original file line number Diff line number Diff 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
121123resource "aws_appautoscaling_policy" "pc_policy" {
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments