Angle Tom
Angle Tom

Reputation: 1130

How to repeat the process when the lambda is failed?

I am trying to use aws lambda to get records from Kinesis and then send it to a web service by http request.

But sometimes the http request is failed because of the network problem, I want all the data. So I have to repeat the process of the lambda.

Anyone knows how to do it?

Upvotes: 0

Views: 336

Answers (2)

mwag
mwag

Reputation: 4065

If you want to de-couple the solution from kinesis, you can use the following construct, assuming cb(err, data) is your callback signature:

var MY_RETRY_WAIT_TIME_IN_MILLISECONDS = 2000; // wait 2 seconds between retries
var MY_MAX_TRIES = 10; // don't try more than 10 times

function invoke(params, cb) { // this will recurse using setTimeout()
  console_err('invoking lamda');

  if(!params.trycount) // this is going to be incremented with each try
    params.trycount = 1;

  var lambda_params = {
    FunctionName: 'mylambdafunction',
    InvocationType: 'RequestResponse',
    LogType: 'Tail',
    Payload: JSON.stringify(params)
  };

  lambda.invoke(lambda_params, function(err, obj){
    if(err) {
      console_err('got an error: ' + err.toString());
      if(err.toString().match(/ServiceException/)) { // make whatever conditions you want here
        if(params.trycount < MY_MAX_TRIES) {
          params.trycount++;
          console_err('Try ' + params.trycount +
                      ' in ' + MY_RETRY_WAIT_TIME_IN_MILLISECONDS/1000 +
                      ' seconds');
          setTimeout(function() { // try again
            invoke(params, cb);
          }, MY_RETRY_WAIT_TIME_IN_MILLISECONDS);
        } else
          cb('trycount exceeded maximum');
      } else
        cb(err); // handle error that you don't want to retry on
    } else {
      console.log("Lambda call succeeded");
      cb(null, obj.Payload); // handle successful call
    }
  });
}

Upvotes: 0

Jonathan Seed
Jonathan Seed

Reputation: 2017

Assuming you are using the Kinesis stream as an event source, the stream will not move forward unless the lambda function executes successfully, meaning that as long as you provide an error in the callback, you dont have to worry about missing data.
For reference, failing out on error would look like this:

exports.handler = function(event, context, callback){
  /* Some logic */
  someRequest(someData, function(err, data){
    if(err)
      return callback(err);
    /* More logic */
    callback();
}

Upvotes: 2

Related Questions