API Gateway with Lambda - "Unable to import module 'server': No module named 'server'"

0

Hello Everyone,

I tried to setup a serverless-api using typescript as per https://github.com/fortejas/example-serverless-python-api on a cloud9 environment The deployment is successful, however when i access the REST API endpoint, I am getting the following error

curl -d POST https://8egyaaan6f.execute-api.ap-southeast-2.amazonaws.com/prod/update {"message": "Internal server error"}

[ERROR] Runtime.ImportModuleError: Unable to import module 'server': No module named 'server'
Traceback (most recent call last):

On further investigation, the error seems to be related to https://github.com/fortejas/example-serverless-python-api/blob/main/lambda-api/server.py Any help is appreciated

Tue Jan 11 05:32:21 UTC 2022 : Endpoint response body before transformations: {"errorMessage": "Unable to import module 'server': No module named 'server'", "errorType": "Runtime.ImportModuleError", "requestId": "0be79d0e-e10b-4651-8838-b4e8dff65143", "stackTrace": []}
Tue Jan 11 05:32:21 UTC 2022 : Lambda execution failed with status 200 due to customer function error: Unable to import module 'server': No module named 'server'. Lambda request id: 0be79d0e-e10b-4651-8838-b4e8dff65143
Tue Jan 11 05:32:21 UTC 2022 : Method completed with status: 502
1 réponse
1
Réponse acceptée

Hi,

From the error message, it seems that the Lambda function is unable to find the Handler method which is in the server.py file. Lambda tries to look for the server.py file but it is unable to find it. You can read more about setting the Python Lambda Handler here in this documentation

Please check the Lambda function, check the folder structure and confirm that the Handler that has been configured has the correct path.

For example, let's say I have the following folder structure in my Lambda function named "PythonLambda":

PythonLambda (This is the top Level folder which will have the same name as the Lambda function)
  -> lambda-api (This is a sub-folder)
      -> server.py (the server.py file is under the folder lambda-api)

If my Lambda handler is server.lambda_handler, I will be getting an identical error as your error:

START RequestId: 1d69b994-d660-4d64-ac74-83f6a659ae5c Version: $LATEST
[ERROR] Runtime.ImportModuleError: Unable to import module 'server': No module named 'server'
Traceback (most recent call last):END RequestId: 1d69b994-d660-4d64-ac74-83f6a659ae5c
REPORT RequestId: 1d69b994-d660-4d64-ac74-83f6a659ae5c	Duration: 1.04 ms	Billed Duration: 2 ms	Memory Size: 128 MB	Max Memory Used: 37 MB	
  • This is because Lambda tries to look for server.py in the top level folder but is unable to find it.

In order to be able to successfully call this Lambda, we must change the handler to lambda-api.server.lambda_handler so that Lambda can find it under the folder.

START RequestId: 3c1b9e59-7ed0-41fe-8207-abc163067f56 Version: $LATEST
END RequestId: 3c1b9e59-7ed0-41fe-8207-abc163067f56
REPORT RequestId: 3c1b9e59-7ed0-41fe-8207-abc163067f56	Duration: 0.90 ms	Billed Duration: 1 ms	Memory Size: 128 MB	Max Memory Used: 37 MB	
AWS
INGÉNIEUR EN ASSISTANCE TECHNIQUE
Ryan_A
répondu il y a 2 ans
profile picture
EXPERT
vérifié il y a 21 heures
  • Thank you, I understand

    However the issue seems to be with CDK deployment because the server.py is not included in the lambda package. In order to verify, I have downloaded the lambda code and checked for server.py, which was missing.

    I was wondering if this could be a CDK issue

  • Thank you Ryan

    I managed to resolve the problems

    1. I think the first issue "no module named server" was due to another code repo in the directory. I started from scratch and then I noticed that python code was not able to find FastAPI
    2. I have setup virtual pip environment and installed the required python packages and it worked
      355 curl -O https://bootstrap.pypa.io/get-pip.py
      356  ls -lrt
      357  sudo python3.7 get-pip.py 
      358  python -m pip --version 
      359  rm get-pip.py 
      360  pipenv
      361  cd lambda-api/
      362  pipen
      363  pipenv
      364  cd ..
      365  virtualenv -p /usr/bin/python3.7 vpy37
      366  source vpy37/bin/activate
      367  pip3 install fastapi
      368  pip3 install boto3
      369  pip3 install Magnum
      370  pip3 install uvicorn
    
    1. then run cdk bootstrap and cdk deploy

Vous n'êtes pas connecté. Se connecter pour publier une réponse.

Une bonne réponse répond clairement à la question, contient des commentaires constructifs et encourage le développement professionnel de la personne qui pose la question.

Instructions pour répondre aux questions