AWS Lambda Function

0

HI, I enrolled in AWS Free Tier to test the AWS services focused on AI engines. My last experiment is the build of an OpenAI Chatbot using a Python env and these packages: aiofiles==23.2.1 fastapi==0.103.2 Jinja2==3.1.2 mangum==0.17.0 openai==0.28.1 python-multipart==0.0.6 I generated a zip file with a dependencies folder and a main.py file. In the dependencies folder, I compressed Python packages and templates for the UI. So I created a layer in AWS Lambda to upload the zip files and a function with this setup:

  • Enable function URL
  • Auth type None
  • in the running settings main.handler
  • in the environment variables the openai api key When I tested this function I got this error: Test Event Name test

Response { "errorMessage": "Unable to import module 'main': No module named 'main'", "errorType": "Runtime.ImportModuleError", "requestId": "7b91ff55-0dab-4d9b-bef0-6310e8f43a49", "stackTrace": [] }

Function Logs START RequestId: 7b91ff55-0dab-4d9b-bef0-6310e8f43a49 Version: $LATEST [ERROR] Runtime.ImportModuleError: Unable to import module 'main': No module named 'main'

can someone help me? Thank You.

PaolaG
asked 7 months ago266 views
3 Answers
0

Hello.

The error message "Unable to import module 'main': No module named 'main'" indicates that AWS Lambda is unable to locate or import your main module, which is supposed to contain the Lambda handler function.

Ensure your Lambda deployment package has the correct directory structure. The file containing your Lambda handler (handler) should be in the root of the ZIP file, not inside a directory (unless you are specifying the directory in the handler configuration).

Here’s a sample directory structure:

|- main.py
|- dependencies/
   |- <Your Dependency Files>
In main.py, you should have a function named handler like so:
def handler(event, context):
    # Your code here

Regards, Andrii

profile picture
EXPERT
answered 7 months ago
  • Hello Andrii S, Here’s the structure of aws_lambda_artifact.zip file: |- main.py |- dependencies/ Folders of all the necessary Python Libraries Templates folder

    Inside main.py: ... app = FastAPI() handler = Mangum(app) templates = Jinja2Templates(directory="templates") chat_responses = [] ...

    So, as you can see, I did not use a function for the handler but I set this handler to run my app through the Mangum handler.

    What do think? What is the problem?

    Thanks, Paola

0

Since you're using Mangum with FastAPI, the handler is set to Mangum(app). The handler specification should be correct as main.handler.

Regards, Andrii

profile picture
EXPERT
answered 6 months ago
0

Runtime for changing handler default setup As you can see in the attached image, I set up the handler in main.handler. So I think this isn't the reason. I'm sorry to ask you for a solution, but I don't know what to do because I've read extensively about the topic. Regards, Paola

PaolaG
answered 6 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions