I know that when a Lambda function fails (for example when there is a time out), it tries to run the function 3 more times again. Is there any way to avoid this behavior? I\
It retries when it is an unhandled failure (an error that you didn't catch) or if you handled it but still told Lambda to retry (e.g. in Node, when you call callback() with a non-null first argument).
To stop it from retrying, you should make sure that any error is handled and you tell Lambda that your invocation finished successfully by returning a non-error (or in Node, calling callback(null, .
To do this you can enclose the entire body of your handler function with a try-catch.
module.exports.handler(event, context, callback) {
try {
// Do you what you want to do.
return callback(null, 'Success')
} catch (err) {
// You probably still want to log it.
console.error(err)
// Return happy despite all the hardships you went through.
return callback(null, 'Still success')
}
}
As for failures due to timeouts there are things you can do.
If it times out outside of your handler, there's usually something wrong with your code (e.g. incorrect database config, etc) that you should look at.
If it times out inside your handler during an invocation, there is something you can do about it.
context.getRemainingTimeInMillis() to know if the Lambda is about to timeout, so you can handle it earlier.