Search code examples
pythonpython-3.xamazon-web-servicesaws-lambdapymongo

Unable to import module 'lambda_function': No module named 'pymongo'


I have followed this guide last year to build my AWS python archive and it was working.

Today i have automated my code deployment and it was not working (i am using shutil.make_archive("package", 'zip', root_dir=path, base_dir="package/", verbose=True) instead of the zip -r package.zip package/ command now.

But when i start doing all these steps manually again and deploy (without the automated code) it's not working anymore.

Here is what my folders look like :

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.


Solution

  • 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