Tom3652
Tom3652

Reputation: 2967

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.

Upvotes: 0

Views: 1923

Answers (2)

Leeroy Hannigan
Leeroy Hannigan

Reputation: 19853

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

Upvotes: 0

Manish sah
Manish sah

Reputation: 26

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

Upvotes: 1

Related Questions