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
질문됨 7달 전283회 조회
3개 답변
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
전문가
답변함 7달 전
  • 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
전문가
답변함 7달 전
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
답변함 7달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠