0

I'm not a Python developer but I got stuck with taking over the maintenance of a Python 3.9 project that currently runs as a cronjob on an EC2 instance. I need to get it running from AWS Lambda. Currently the crontab on the EC2 instance invokes a cronjob/run.py script that looks a little like this:

import os
import sys

from dotenv import load_dotenv
load_dotenv()
sync_events = get_sync_events()
# lots more stuff down here

The important thing here is that there is no __main__ method invoked. The crontab just treats this Python source file like a script and executes it from top to bottom.

My understanding is that the Lambda Handler needs a main method to be invoked. So I need a way to run the existing cronjob/run.py (that again, has no main entry point) from inside the Lambda Handler, somehow:

def lambda_handler(event, context):
    try:
        # run everything thats in cronjob/run.py right here
        raise e
    except Exception as e:
        raise e

if __name__ == '__main__':
    lambda_handler(None, None)

So my question: do I need my Lambda Handler to have a __main__ method like the above, or is it possible to configure my Lambda to just call cronjob/run.py directly? If not, what are the best options here? Thanks in advance!

1
  • No, you expressly should not have an __main__ test that runs the Lambda function handler. The AWS Lambda service will invoke the configured Lambda function handler at the relevant time, passing event and context. Use them as you need. Commented Aug 8, 2022 at 19:10

1 Answer 1

2

do I need my Lambda Handler to have a main method

No, you don't.

If you just want to run run.py with lambda, you can keep things simple and just use:

import os
import sys
from dotenv import load_dotenv

def main(event, context):         
    load_dotenv()
    sync_events = get_sync_events()
    # lots more stuff down here

and configure the lambda function to have run.main as the handler.

The name of the handler function, in this case main, can be anything, but it must have event and context as arguments.

You can find more information on lambda handler here: https://docs.aws.amazon.com/lambda/latest/dg/python-handler.html

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

6 Comments

Thanks @Paolo (+1) - just to confirm: (1) if I add the def main(event, context): return to the bottom of the cronjob/run.py sourcefile, that will make that file "runnable" from Lambda's perspective?
(2) I have the entire Python project in an S3 bucket. In reality the cronjob/run.py source file references source code in lots of other Python files as well (and not just those inside the cronjob/ dir). Are Lambdas supposed to just be single Python/JS files? Or is there a way to fetch the entire project from S3, "deploy" it to the Lambda, and configure cronjob/run.py main method as the starting point?
@hotmeatballsoup (1), yes. (2), you can have multiple Python files in a lambda function. There's no automated way to do that; pull the files from S3 and you could use something like sam to package and deploy your lambda docs.aws.amazon.com/serverless-application-model/latest/…
(you don't have to use sam, but it will make your life easier)
Thank you again @Paolo (+1 again) -- what's you're recommendation for "pulling" the files from S3? Use the aws lambda update-function-code CLI tool perhaps?
|

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.