Retrying in AWS Lambda

I am writing a Lambda function that talks to an endpoint. If error 500 occurs, I would like the function to repeat itself multiple times.

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

:

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 right thing with the variable context

is.

After the code I provided above, should I context.fail()

? Or should I wait for the appearance context.succeed()

or context.fail()

at a later iteration of this retry process?

I'm just having a hard time deciding if each one needs to be resolved context

at the level of the original exports.handler

that it came up with, or if context

any level can be resolved and that will resolve it for all Lambda execution.

Thanks for any advice.

+3


source to share


1 answer


Take a look here:

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

The feature will work up to 3 times before the Lambda surrenders.

If you call "context.succeed" the lambda will not try again (assuming your function was successful).



Calling "context.fail" will try your function again.

One note: you must be careful to call "context.succeed" or "context.fail" at the right time. If you are calling with either pending callbacks that node has not yet processed, then your Lambda function will be suspended earlier and some of your code might work. So the best place to call context.succeed or context.fail is in the callback when you know no other processing should happen.

Also worth checking out this article:

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

+8


source







All Articles