Home > Software design >  Unable to import module 'lambda_function': No module named 'pymongo'
Unable to import module 'lambda_function': No module named 'pymongo'

Time:12-30

I have followed this enter image description here

enter image description here

The only thing that has changed since the last time it was working is the pymongo version which was 3.12 instead of 4.3.3.

My handler is :

def lambda_handler(event, context):
    print(event)

The error i am getting from CloudWatch :

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

What am i doing wrong ?

EDIT :

I have found an old dependency package that is working, but that is using more than just pymongo[srv] so i will check what may be different but the error comes from the dependencies.

CodePudding user response:

For lambda to find your external dependency, all your dependency must be inside audio-handler folder and not package. By default, lambda looks for dependency in the root directory.

audio-handler
 |-bson
 |-pymongo
 .
 .
 .
 |-lambda_function.py

Here is the video on how to create your deployment package and deploy it on aws lambda https://www.youtube.com/watch?v=Jtlxf_kn5zY

CodePudding user response:

I would strongly advise that you use a Lambda Layer to import external modules, ensuring your packaged components are at the root of the project and not within a directory named package.

Add External Python Libraries to AWS Lambda using Lambda Layers https://www.linkedin.com/pulse/add-external-python-libraries-aws-lambda-using-layers-gabe-olokun

  • Related