Deleting Old and Unused AWS Lambda Function Versions using Typescript
Code
Below is the AWS SDK example that utilizes Typescript to delete old and unused AWS Lambda function versions:
const AWS = require('aws-sdk'); const lambda = new AWS.Lambda(); async function deleteOldLambdaVersions(FunctionName) { const versions = await lambda.listVersionsByFunction({ FunctionName }).promise(); const promises = versions.Versions.map(async(version) => { if(version.Version !== '$LATEST') { const params = { FunctionName, Qualifier: version.Version }; await lambda.deleteFunction(params).promise(); console.log(`Deleted version ${version.Version}`); } }); return Promise.all(promises); } deleteOldLambdaVersions('YOUR_FUNCTION_NAME').then(() => console.log('Deleted all old versions'));
Please replace 'YOUR_FUNCTION_NAME'
with the name of your Lambda function.
Detailed Code Explanation
In the script above, deleteOldLambdaVersions
is an asynchronous function that removes the older versions of a specified Lambda function. It calls the listVersionsByFunction
method to return a list of the AWS Lambda function's versions. Then it traverses the list and runs the deleteFunction
method with appropriate parameters for each version except '$LATEST', which is technically the latest published version or the function's $LATEST
version.
Expected Output Format
There will be no JSON output from this operation. Instead, the console will log every deleted version number, and at the end display 'Deleted all old versions'.
Deleted version 1 Deleted version 2 ... Deleted all old versions
Considerations & Caveats
- Only delete versions that you are certain will no longer be used. Once a version is deleted, it cannot be recovered.
- This script will not delete the
$LATEST
version, as it is considered the most recent and generally should not be removed. - Ensure the Lambda function specified exists and is spelled correctly, or the code will throw an error.
- API call rate limits: AWS has per-client/per-region default service throttle that could cause RequestLimitExceeded errors if many API calls are made in a short period.
Required IAM Permissions and Example Policy
To use this script, your IAM role needs to have Lambda permissions lambda:ListVersionsByFunction
and lambda:DeleteFunction
.
Here is an example policy:
{ "Version": "2012-10-17", "Statement": [ { "Sid": "LambdaVersionManagement", "Effect": "Allow", "Action": [ "lambda:ListVersionsByFunction", "lambda:DeleteFunction" ], "Resource": "*" } ] }
FAQ
Q: Can I recover a version accidentally deleted by the script?
A: No, once a Lambda function version is deleted, it cannot be restored.
Q: Will this script delete my $LATEST
function version?
A: No, this script leaves the $LATEST
version intact.
Q: What if I run this script and the function name does not exist?
A: The script will throw an error if you run it with a function name that does not exist.
Q: Can I exceed API request quotas with this script?
A: If you're controlling numerous Lambda functions and processing multiple API calls concurrently, you might potentially hit the API request limit. In such cases, consider incorporating appropriate rate limit control in your code.