3

Lambda AWS shut down when I throw an exception?

In my code, I throw an exception when an illegal state happens. I want to know how Lambda deals with it if the service shut down or not.

I can't find any reference to it, in their documentation, it's all about handling the errors/exception. But I want to know if a unhandled exception should shut down my Lambda service.

3 Answers 3

3

What AWS Lambda does when it encounters an exception depends on how it got invoked. In short: If it got invoked synchronously an error is returned to the caller, if it got invoked asynchronously retries happen. For more details please check out https://docs.aws.amazon.com/lambda/latest/dg/retries-on-errors.html

As AWS Lambda's execution model is a stateless an exception only affects the current invocation. Following invocations are handled properly as if there was no exception.

(Disclaimer: AWS Lambda is only stateless to a certain extend, as it reuses existing containers. I believe that's not relevant to your question, but if you want to learn more about it I suggest the following article: https://aws.amazon.com/de/blogs/compute/container-reuse-in-lambda/)

Sign up to request clarification or add additional context in comments.

1 Comment

I don't believe it's documented, but based on testing (in Node), I suspect that Lambda actually destroys the container and won't reuse it if an exception is thrown -- instead, it would spin up a new container for the next invocation, if one wasn't already available. This may vary by language, but it would make sense if Lambda assumes the process is unstable and refuses to reuse it, starting fresh, instead. It's easy enough to prove with a global counter (created outside the handler and incremented inside) how many times a particular container has seen a function invocation.
2

I faced a lot of issues dealing with this as not much documentation or blog post are there regarding this. Finally this is what i learned:

  1. Always put try/catch in all the functions, no exceptions to this rule. Whether its entry point function to lambda or any inner or nested function.
  2. Throw exception and catch in parent. Bubble up till u reach the entry point function.
  3. Nodejs errors cannot be JSON.stringify. I was doing that so that i can send the error response back to APIgateway. you will notice that though you can console.log the error but you cant JSON.stringify, the moment you do it, lambda will crash without any error. To solve this do the following:

    function GetErrorObject(data) {
      let dataToSend = data;
    
      //check if error object
      if (data.hasOwnProperty('stack')) {
        dataToSend = JSON.parse(JSON.stringify(data, Object.getOwnPropertyNames(data)));
      }
      return { message: dataToSend};
    }
    

This is only demo, modifiy based on your requirement. idea is a read every property of error object and convert to a valid JSON. Hope it helps

Comments

1

Lambda invokes your function via triggers (e.g. an S3 PUT event)

Exceptions that propagate outside your code will be caught and that invocation will be marked as an "Error", you will see a metric for this in CloudWatch.

This won't affect future invocations of your function i.e. you could trigger the lambda again and it will run your code again.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.