Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda - How to stop retries when there is a failure

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' have been reading the documentation, but didn't find anyting about this.

Thanks!

like image 651
AleGallagher Avatar asked Mar 02 '18 12:03

AleGallagher


People also ask

Do lambdas retry on failure?

If your function throws an error, the Lambda service retries your function. Since the same event may be received more than once, functions should be designed to be idempotent . This means that receiving the same event multiple times does not change the result beyond the first time the event was received.

Does AWS Lambda retry on timeout?

Short description. There are three reasons why retry and timeout issues occur when invoking a Lambda function with an AWS SDK: A remote API is unreachable or takes too long to respond to an API call. The API call doesn't get a response within the socket timeout.

How many times will Lambda Retry?

Maximum Retry Attempts When a function returns an error after execution, Lambda attempts to run it two more times by default. With Maximum Retry Attempts, you can customize the maximum number of retries from 0 to 2. This gives you the option to continue processing new events with fewer or no retries.


2 Answers

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, <any>).

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.

  • If it's an HTTP request, you should set the timeout of the request to be shorter than your Lambda's timeout, this way it will fail properly and you can catch it.
  • If it's a database connection, you can probably set a timeout using the library that you're using.
  • If it's your logic that times out, you can upgrade the Lambda to use higher memory-CPU to make it faster. You can also check context.getRemainingTimeInMillis() to know if the Lambda is about to timeout, so you can handle it earlier.
like image 145
Noel Llevares Avatar answered Sep 17 '22 16:09

Noel Llevares


EDITED: It's now possible to change the lambda settings to disable retries (see announcement)


Original answer:

There isn't a way to disable retry behavior of Lambda functions. I suggest two options to deal with that:

  1. Make your Lambda able to handle retry cases correctly. You can use context.AwsRequestId (or corresponding field) that will be the same when a Lambda is retried.
  2. Put your Lambda inside a state machine (using AWS Step Functions). You can only then disable retries. This blog post I've written gives a more general explanation.
like image 42
Ronyis Avatar answered Sep 20 '22 16:09

Ronyis