11

Inside a certain function I want to stop the whole Lambda process but not trigger an error. I've tried looking at the context variable to stop it or just calling exit() but each time it's treated as an error which I don't want to track.

How can I end successfully a Lambda process in Python?

7
  • 1
    Can you please post the code that throws error. Commented Aug 10, 2018 at 7:24
  • How to exit from Python without traceback? Commented Aug 10, 2018 at 7:40
  • 1
    @krishna just exit() and it logs Process exited before completing request and logs it as an error Commented Aug 10, 2018 at 8:45
  • @JohnRotenstein I've tried both sys.exit(0) and os._exit(1) and they are still counted as errors Commented Aug 10, 2018 at 11:00
  • 1
    Lambda loads and executes your function's code like a module. That's why you have to create a handler function instead of just using a normal script entry point. When you call exit() like that you are killing everything instead of returning from your function like the Lambda server expects. In other words, exit() is always going to result in that error message. You need to return from your function instead. Commented Aug 10, 2018 at 17:58

1 Answer 1

15

In AWS Lambda you define a handler function and in Python a function just needs to return in order to complete successfully, where return implies return None.

What you've done is correct, just have multiple return points in that handler function. You can always log messages for different reasons for the function completing if needed.

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

3 Comments

From memory it wasn't easily possible (I refactored the code to make it work) because it was a function called from handler that would have the condition to halt the function. I believe what I did was return a specific string or something that would be checked from the calling handler function and then would return based on that. I guess I'm curious why the NodeJS version of Lambda has this ability but Python doesn't. I did get it to work with a refactor a while ago so my memory isn't great with it.
In that other SO question there are legacy methods mentioned, e.g. context.done(), in the newer nodejs SDK you should use a callback instead docs.aws.amazon.com/lambda/latest/dg/… That callback is optional. The docs mention that it will implicitly execute that callback if you don't, but this is not the end of execution, it's just for easy logging to CloudWatch. Javascript functions will still complete at the end of event loop either by returning an explicit value, an implicit default undefined or throw an exception, same as Python.
Behind the scenes, the Lambda runner is just some python (or node or other lang) function that wraps your function, it calls your function, passing it an event & context, and waits for your function to return or throw an exception. When you call other functions inside your handler, you are doing the same thing, eventually those other functions must complete and return to the handler. The entry and exit point for the entire lambda is, and can only be, the handler function.

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.