Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
781 views
in Technique[技术] by (71.8m points)

amazon web services - 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...