Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda import module error in python

People also ask

Could not load Lambda handler function due to error Cannot find module AWS SDK?

The "Cannot find module" occurs when a lambda function is trying to access a module which is not available in the function's execution runtime. The most common causes for the error are: zipping the wrong files, e.g. zipping a directory instead of the contents of the directory.

Can I use Python packages with AWS Lambda?

Lambda supports Python, which is a great option if you've got experience using it. However, one of the downsides to Lambda is that by default you won't be able to import your trusted packages, like Pandas. I'm going to show you the easiest way of importing any package you need for your Lambda function.


Error was due to file name of the lambda function. While creating the lambda function it will ask for Lambda function handler. You have to name it Python_File_Name.Method_Name. In this scenario, I named it lambda.lambda_handler (lambda.py is the file name).

Please find below the snapshot. enter image description here


If you are uploading a zip file. Make sure that you are zipping the contents of the directory and not the directory itself.


Another source of this problem is the permissions on the file that is zipped. It MUST be at least world-wide readable. (min chmod 444)

I ran the following on the python file before zipping it and it worked fine.

chmod u=rwx,go=r

I found Nithin's answer very helpful. Here is a specific walk-through:

Look-up these values:

  1. The name of the lambda_handler function in your python script. The name used in the AWS examples is lambda_handler looking like def lambda_handler(event, context). In this case, the value is lambda_handler
  2. In the Lambda dashboard, find the name of the Handler in the Handler text-box in the Configuration section in the lambda dashboard for the function (shown in Nithin's screenshot). My default name was lambda_function.lambda_handler.
  3. The name of your python script. Let's say it's cool.py

With these values, you would need to rename the handler (shown in the screenshot) to cool.lambda_handler. This is one way to get rid of the "Unable to import module lambda_function" errorMessage. If you were to rename the handler in your python script to sup then you'd need to rename the handler in the lambda dashboard to cool.sup


Here's a quick step through.

Assume you have a folder called deploy, with your lambda file inside call lambda_function.py. Let's assume this file looks something like this. (p1 and p2 represent third-party packages.)

import p1
import p2

def lambda_handler(event, context):
    # more code here

    return {
        "status": 200,
        "body" : "Hello from Lambda!",
    }

For every third-party dependency, you need to pip install <third-party-package> --target . from within the deploy folder.

pip install p1 --target .
pip install p2 --target .

Once you've done this, here's what your structure should look like.

deploy/
├── lambda_function.py
├── p1/
│   ├── __init__.py
│   ├── a.py
│   ├── b.py
│   └── c.py
└── p2/
    ├── __init__.py
    ├── x.py
    ├── y.py
    └── z.py

Finally, you need to zip all the contents within the deploy folder to a compressed file. On a Mac or Linux, the command would look like zip -r ../deploy.zip * from within the deploy folder. Note that the -r flag is for recursive subfolders.

The structure of the file zip file should mirror the original folder.

deploy.zip/
├── lambda_function.py
├── p1/
│   ├── __init__.py
│   ├── a.py
│   ├── b.py
│   └── c.py
└── p2/
    ├── __init__.py
    ├── x.py
    ├── y.py
    └── z.py

Upload the zip file and specify the <file_name>.<function_name> for Lambda to enter into your process, such as lambda_function.lambda_handler for the example above.


There are just so many gotchas when creating deployment packages for AWS Lambda (for Python). I have spent hours and hours on debugging sessions until I found a formula that rarely fails.

I have created a script that automates the entire process and therefore makes it less error prone. I have also wrote tutorial that explains how everything works. You may want to check it out:

Hassle-Free Python Lambda Deployment [Tutorial + Script]


I found this hard way after trying all of the solutions above. If you're using sub-directories in the zip file, ensure you include the __init__.py file in each of the sub-directories and that worked for me.