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?

Rudiger
  • 6,749
  • 13
  • 51
  • 102
  • 1
    Can you please post the code that throws error. – Krishna Aug 10 '18 at 07:24
  • [How to exit from Python without traceback?](https://stackoverflow.com/q/1187970/174777) – John Rotenstein Aug 10 '18 at 07:40
  • 1
    @krishna just `exit()` and it logs `Process exited before completing request` and logs it as an error – Rudiger Aug 10 '18 at 08:45
  • @JohnRotenstein I've tried both sys.exit(0) and os._exit(1) and they are still counted as errors – Rudiger Aug 10 '18 at 11:00
  • I've rewritten the function so it returns from the main function which has solved it being marked as "errors" but would still be good to know. – Rudiger Aug 10 '18 at 11:30
  • 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. – Mark B Aug 10 '18 at 17:58
  • @MarkB The option is provided in NodeJS via the `context` https://stackoverflow.com/questions/38660024/how-to-make-aws-lambda-stop-execution however it's not provided in the `context` of Python from what I can see. The same flow happens in NodeJS as Python so why does NodeJS not throw an error and Python does? – Rudiger Aug 11 '18 at 00:38

1 Answers1

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.

Davos
  • 5,066
  • 42
  • 66
  • 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. – Rudiger Nov 15 '18 at 00:40
  • 1
    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 https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html 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. – Davos Nov 15 '18 at 05:08
  • 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. – Davos Nov 15 '18 at 05:13