AWS Lambda import module error in python

I am creating a AWS Lambda python deployment package. I am using one external dependency requests . I installed the external dependency using the AWS documentation Below is my python code.

import requests

print('Loading function')

s3 = boto3.client('s3')

def lambda_handler(event, context):
    #print("Received event: " + json.dumps(event, indent=2))

    # Get the object from the event and show its content type
    bucket = event['Records'][0]['s3']['bucket']['name']
    key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key']).decode('utf8')
        response = s3.get_object(Bucket=bucket, Key=key)
        s3.download_file(bucket,key, '/tmp/data.txt')
        lines = [line.rstrip('n') for line in open('/tmp/data.txt')]
        for line in lines:
        print("CONTENT TYPE: " + response['ContentType'])
        return response['ContentType']
    except Exception as e:
        print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
        raise e

Created the Zip the content of the project-dir directory and uploaded to the lambda(Zip the directory content, not the directory). When I am execute the function I am getting the below mentioned error.

START RequestId: 9e64e2c7-d0c3-11e5-b34e-75c7fb49d058 Version: $LATEST
**Unable to import module 'lambda_function': No module named lambda_function**

END RequestId: 9e64e2c7-d0c3-11e5-b34e-75c7fb49d058
REPORT RequestId: 9e64e2c7-d0c3-11e5-b34e-75c7fb49d058  Duration: 19.63 ms  Billed Duration: 100 ms     Memory Size: 128 MB Max Memory Used: 9 MB

Kindly help me to debug the error.

Stack Overflow Asked by Nithin K Anil on January 28, 2021

21 Answers

21 Answers

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 as your Python_File_Name.Method_Name. In this scenario I named it as lambda.lambda_handler ( is the file name).

Please find below the snapshot. enter image description here

Correct answer by Nithin K Anil on January 28, 2021

The issue here that the Python version used to build your Lambda function dependencies (on your own machine) is different than the selected Python version for your Lambda function. This case is common especially if the Numpy library part of your dependencies.

Example: Your machine's python version: 3.6 ---> Lambda python version 3.6

Answered by Sharhabeel Hamdan on January 28, 2021

Please add below one after Import requests

import boto3

What I can see that is missing in your code.

Answered by Mrinal on January 28, 2021

Actually go to the main folder (deployment package)that you want to zip,

Inside that folder select all files and then create the zip and upload that zip

Answered by MUHAMMAD ZEESHAN on January 28, 2021

My problem was that the .py file and dependencies were not in the zip's "root" directory. e.g the path of libraries and lambda function .py must be:

<name of library>/foo/bar/


/foo/bar/<name of library>/foo2/bar2

For example:

drwxr-xr-x  3.0 unx        0 bx stor 20-Apr-17 19:43 boto3/ec2/__pycache__/
-rw-r--r--  3.0 unx      192 bx defX 20-Apr-17 19:43 boto3/ec2/__pycache__/__init__.cpython-37.pyc
-rw-r--r--  3.0 unx      758 bx defX 20-Apr-17 19:43 boto3/ec2/__pycache__/deletetags.cpython-37.pyc
-rw-r--r--  3.0 unx      965 bx defX 20-Apr-17 19:43 boto3/ec2/__pycache__/createtags.cpython-37.pyc
-rw-r--r--  3.0 unx     7781 tx defN 20-Apr-17 20:33

Answered by lobi on January 28, 2021

Make sure that you are zipping all dependencies in a folder structure as python/[Your All Dependencies] to make it work as per this documentation.

Answered by Sachin Kumar on January 28, 2021

You can configure your Lambda function to pull in additional code and content in the form of layers. A layer is a ZIP archive that contains libraries, a custom runtime, or other dependencies. With layers, you can use libraries in your function without needing to include them in your deployment package. Layers let you keep your deployment package small, which makes development easier.



Answered by Rahul Satal on January 28, 2021

Sharing my solution for the same issue, just in case it helps anyone.

Issue: I got error: "[ERROR] Runtime.ImportModuleError: Unable to import module 'lambda_function': No module named 'StringIO'" while executing aws-big-data-blog code[1] provided in AWS article[2].

Solution: Changed Runtime from Python 3.7 to Python 2.7

[1] — [2] —

Answered by user72789 on January 28, 2021

Here's a quick step through.

Assume you have a folder called deploy, with your lambda file inside call 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.

├── p1/
│   ├──
│   ├──
│   ├──
│   └──
└── p2/

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 ../ * 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.
├── p1/
│   ├──
│   ├──
│   ├──
│   └──
└── p2/

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.

Answered by openwonk on January 28, 2021

A perspective from 2019:

AWS Lambda now supports Python 3.7, which many people (including myself) choose to use as the runtime for inline lambdas.

Then I had to import an external dependency and I followed AWS Docs as the OP referred to. (local install --> zip --> upload).

I had the import module error and I realised my local PC had Python 2.7 as the default Python. When I invoked pip, it installed my dependency for Python 2.7.

So I switched locally to the Python version that matches the selected runtime version in the lambda console and then re-installed the external dependencies. This solved the problem for me. E.g.:

$ python3 -m pip install --target path/to/lambda_file <external_dependency_name>

Answered by l001d on January 28, 2021

In lambda_handler the format must be lambda_filename.lambda_functionName. Supposing you want to run the lambda_handler function and it's in, then your handler format is lambda_function.lambda_handler.

Another reason for getting this error is module dependencies.

Your must be in the root directory of the zip file.

Answered by PKP on January 28, 2021

@nithin, AWS released layers concept inside Lambda functions. You can create your layer and there you can upload as much as libraries and then you can connect the layer with the lambda functions. For more details:

Answered by muTheTechie on January 28, 2021

You need to zip all the requirements, use this script

#!/usr/bin/env bash
mkdir package
pip install -r requirements.txt --target package
cat $1 > package/
cd package
zip -r9 "../" .
cd ..
rm -rf package

use with: <python_file>

Answered by Uri Goren on January 28, 2021

I ran into the same issue, this was an exercise as part of a tutorial on if I'm not wrong. The mistake I made was not selecting the runtime as Python 3.6 which is an option in the lamda function console.

Answered by Nadeem on January 28, 2021

No need to do that mess.

use python-lambda

with single command pylambda deploy it will automatically deploy your function

Answered by softmarshmallow on January 28, 2021

I had the error too. Turn out that my zip file include the code parent folder. When I unzip and inspect the zip file, the lambda_function file is under parent folder ./lambda.

Use the zip command, fix the error:

zip -r ../ ./*

Answered by Joe on January 28, 2021

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 file in each of the sub-directories and that worked for me.

Answered by KApuri on January 28, 2021

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

Answered by 2ank3th on January 28, 2021

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]

Answered by joarleymoraes on January 28, 2021

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

Answered by Catalin Ciurea on January 28, 2021

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 ""

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"

Answered by user3303554 on January 28, 2021

Add your own answers!

Related Questions

Find a cell containing a specific date

1  Asked on December 22, 2021


Jeykll file to import not found or unreadable: base

1  Asked on December 22, 2021 by kyu96


Unable to remove event listener from document

1  Asked on December 22, 2021 by prawn


Invalid result adding the number of 1s in a binary number

2  Asked on December 20, 2021 by aworeham


How to print the elements of 3 different arrays by the order?

2  Asked on December 20, 2021 by fuzzyfiso


Finding string elements of an array in another array

5  Asked on December 20, 2021 by p-emt


How to plot several columns on the same line chart in R?

2  Asked on December 20, 2021 by user13953317


Is there a way to craft URLs to fill forms faster?

1  Asked on December 20, 2021 by rosalind-goh


infinite loop while using useEffect and redux store

0  Asked on December 20, 2021 by susanta


Cannot convert textfield into string

1  Asked on December 20, 2021 by anuj-amin


Is there multiple ways to cin vector elements?

1  Asked on December 20, 2021 by 2kbummer


Ask a Question

Get help from others!

© 2022 All rights reserved.