September 12, 2023

Cleaning up AWS Lambda functions on unsupported node.js runtimes

node.js drops support for major versions about once a year. AWS will let you know by email if you have any lambdas configured to use soon-to-be-unsupported runtimes. I don't like getting those emails, and I don't want to get a second reminder. The commands below automate cleaning up lambdas on unsupported runtimes.

The email provides a list command to see what we're dealing with. Run it to get a space-separated list of ARNs with function name and version (output is formatted multiline below for readability).

aws lambda list-functions --function-version ALL --region us-east-1 --output text \
    --query "Functions[?Runtime=='nodejs14.x'].FunctionArn"

# arn:aws:lambda:us-east-1:668334381016:function:serverlessrepo-notes-ParseAuthHandler-MAMZ03R4QCXW:2
# arn:aws:lambda:us-east-1:668334381016:function:serverlessrepo-notes-RefreshAuthHandler-1C79RR5JD1ZE9:2

Those are candidates for deletion. But first, check and make sure they're not, you know, in use. In this case I had already updated the serverless application and there are later versions of all of the above. To check:

aws lambda list-functions --function-version ALL --region us-east-1 --output text \
    --query "Functions[?FunctionName=='serverlessrepo-notes-ParseAuthHandler-MAMZ03R4QCXW'].FunctionArn"
# arn:aws:lambda:us-east-1:668334381016:function:serverlessrepo-notes-ParseAuthHandler-MAMZ03R4QCXW:$LATEST
# arn:aws:lambda:us-east-1:668334381016:function:serverlessrepo-notes-ParseAuthHandler-MAMZ03R4QCXW:2
# arn:aws:lambda:us-east-1:668334381016:function:serverlessrepo-notes-ParseAuthHandler-MAMZ03R4QCXW:3
# arn:aws:lambda:us-east-1:668334381016:function:serverlessrepo-notes-ParseAuthHandler-MAMZ03R4QCXW:4
# arn:aws:lambda:us-east-1:668334381016:function:serverlessrepo-notes-ParseAuthHandler-MAMZ03R4QCXW:5
# arn:aws:lambda:us-east-1:668334381016:function:serverlessrepo-notes-ParseAuthHandler-MAMZ03R4QCXW:6
# arn:aws:lambda:us-east-1:668334381016:function:serverlessrepo-notes-ParseAuthHandler-MAMZ03R4QCXW:7

aws lambda delete-function takes one function name at a time. Test piping to xargs with limit one:

aws lambda list-functions --function-version ALL --region us-east-1 --output text \
    --query "Functions[?Runtime=='nodejs14.x'].FunctionArn" \
    | xargs -n1 echo TO DELETE: 

# TO DELETE: arn:aws:lambda:us-east-1:668334381016:function:serverlessrepo-notes-ParseAuthHandler-MAMZ03R4QCXW:2
# TO DELETE: arn:aws:lambda:us-east-1:668334381016:function:serverlessrepo-notes-RefreshAuthHandler-1C79RR5JD1ZE9:2

And delete:

aws lambda list-functions --function-version ALL --region us-east-1 --output text \
    --query "Functions[?Runtime=='nodejs14.x'].FunctionArn" \
    | xargs -n1 aws lambda delete-function --region us-east-1 --function-name

RSS

Creative Commons License This work is licensed under a Creative Commons Attribution 4.0 International License.