Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retries in AWS Lambda

I'm writing a Lambda function which communicates with an endpoint. If a 500 error occurs, I'd like the function to retry itself several times.

I was hoping to do something like this inside my exports.handler function:

exports.handler = function(event, context){  ...
  if (!error && response.statusCode >= 500 && response.statusCode < 600) {
    if (event.retries <= 5) {
      setTimeout(exports.handler(event, context), 60000);
    }
  }...

I'm wondering what the correct thing to do with the context variable is.

After the code I've cited above, should I context.fail()? Or should I wait for context.succeed() or context.fail() to occur in a later iteration of this retry process?

I'm just having a hard time deciding if each context needs to be resolved at the level of the original exports.handler that it appeared in, or if the context can be resolved and any level and that will resolve it for the whole Lambda execution.

Thanks for any advice.

like image 519
rschwieb Avatar asked May 19 '15 14:05

rschwieb


1 Answers

Take a look here:

http://aws.amazon.com/lambda/faqs/

The function will run up to 3 times before Lambda gives up.

If you call "context.succeed", lambda will not retry, however (it assumes your function was successful).

Calling "context.fail" will cause your function to be retried.

One thing to note: you have to be careful to call "context.succeed" or "context.fail" at the right time. If you call either with pending callbacks that node hasn't processed yet, then your Lambda function will be suspended early, and some of your code may run. So, the best place to call context.succeed or context.fail is in a callback, at a point where you know that no other processing needs to happen.

It's also worth checking out this article as well:

https://aws.amazon.com/blogs/compute/container-reuse-in-lambda/

like image 122
Scott Wisniewski Avatar answered Oct 13 '22 23:10

Scott Wisniewski