Reputation: 1618
I am new to AWS SDK for JavaScript. I need to check if a Lambda function exists before I trigger execute on it.
I tried the following:
Each time I make a lambda.getFunctionConfiguration()
using Function name I have to execute. If it returns an Error, the function doesn't exist. If I get meta, I continue with execution.
I feel this is not a cleaner way to handle this. Is there any other way to check if Lambda exists with the name?
Thanks in advance.
Upvotes: 2
Views: 3310
Reputation: 4114
Here is how to check if function exists with listFunctions()
SDK method (proposed by helloV) in more async
way.
const {Lambda} = require('aws-sdk');
const lambda = new Lambda({apiVersion: '2015-03-31'});
const lambdaExists = async (fName, marker = null) => {
try {
const {Functions, NextMarker} = await lambda
.listFunctions({
Marker: marker || '',
MaxItems: 50
})
.promise();
const exists = Functions.map(({FunctionName}) => FunctionName).indexOf(fName) !== -1;
return exists || NextMarker;
} catch (e) {
// Handle with care
console.log(e.toString());
return e;
}
};
Function returns true
if function name is found in current iteration or NextMarker
(string) if not. Iterations end when there is no NextMarker
provided in received response.
Note: This function is a generic representation of possible way of solving the issue. Not fully tested in real conditions and could need some corrections to be used in your code.
Upvotes: 0
Reputation: 52393
You can use ListFunctions JavaScript API and check if the lambda
function is in the output.
listFunctions(params = {}, callback) ⇒ AWS.Request
Returns a list of your Lambda functions. For each function, the response includes the function configuration information. You must use GetFunction to retrieve the code for your function.
Corresponding CLI: aws lambda list-functions
Upvotes: 2