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

前端 未结 9 1287
面向向阳花
面向向阳花 2020-12-05 06:49

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\

相关标签:
9条回答
  • 2020-12-05 07:20

    If you are using Python, I recommend you to follow this thread.

    If you are using Java, inside handleRequest method add this lines:

    ClientConfiguration config = new ClientConfiguration();
    config.setMaxErrorRetry(0);
    
    0 讨论(0)
  • 2020-12-05 07:25

    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.
    0 讨论(0)
  • 2020-12-05 07:25

    If using python then need to put the code in try/catch block as well however there will be no callback. For example:

    try:
        do_something()
    except Exception as e:
        print('Error: ' + str(e))
    

    Since the error has been handled, Lambda will not retry.

    0 讨论(0)
  • 2020-12-05 07:27

    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.
    0 讨论(0)
  • 2020-12-05 07:29

    Use below code to identify and stop retry when there is a failure.

      exports.handler = (event, context, callback) => {
          context.callbackWaitsForEmptyEventLoop = false;
          let lastReqId;
          if (lastReqId == context.awsRequestId) {
            console.log("Lambda auto retry detected. Aborting");// you can write your own logic to decide what to do with the failure data
            return context.succeed();
          } else {
            console.log("new context");
            lastReqId = context.awsRequestId;
          }
        };
    

    you can read more about it here

    0 讨论(0)
  • 2020-12-05 07:33

    Since November 2019, it is possible to set the retry count to 0.

    That means the lambda will not be retried on error.

    0 讨论(0)
提交回复
热议问题