How do I resolve the "Unable to import module" error that I receive when I run Lambda code in Python?

Last updated: 2022-04-12

I receive an "Unable to import module" error when I try to run my AWS Lambda code in Python. How do I resolve the error?

Short description

You typically receive this error when your Lambda environment can't find the specified library in the Python code. This is because Lambda isn't prepackaged with all Python libraries.

To resolve this error, create a deployment package or Lambda layer that includes the libraries that you want to use in your Python code for Lambda.

Important: Make sure that you put the library that you import for Python inside the /python folder.

Resolution

Note: The following steps show you how to create a Lambda layer rather than a deployment package. This is because you can reuse the Lambda layer across multiple Lambda functions. Each Lambda runtime adds specific /opt directory folders to the PATH variable. If the layer uses the same folder structure, then your Lambda function's code can access the layer content without specifying the path.

It's a best practice to create a Lambda layer on the same operating system that your Lambda runtime is based on. For example, Python 3.8 is based on an Amazon Linux 2 Amazon Machine Image (AMI). However, Python 3.7 and Python 3.6 are based on the Amazon Linux AMI.

To create a Lambda layer for a Python 3.8 library, do the following:

1.    In the AWS Cloud9 console, create an Amazon Elastic Compute Cloud (Amazon EC2) instance with Amazon Linux 2 AMI. For instructions, see Creating an EC2 environment in the AWS Cloud9 User Guide.

2.    Create an AWS Identity and Access Management (IAM) policy that grants permissions to call the PublishLayerVersion API operation.

Example IAM policy statement that grants permissions to call the PublishLayerVersion API operation

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "lambda:PublishLayerVersion",
            "Resource": "*"
        }
    ]
}

3.    Create an IAM role and attach the IAM policy to the role. Then, attach the IAM role to the Amazon EC2 instance.

Note: Your EC2 instance now has permissions to upload Lambda layers for the PublishLayerVersion API call.

4.    Open your AWS Cloud9 Amazon EC2 environment. Then, install Python 3.8 and pip3 by running the following commands:

$ sudo amazon-linux-extras install python3.8
$ curl -O https://bootstrap.pypa.io/get-pip.py
$ python3.8 get-pip.py --user

5.    Create a python folder by running the following command:

$ mkdir python

6.    Install the Pandas library files into the python folder by running the following command:

Important: Replace Pandas with the name of the Python library that you want to import.

$ python3.8 -m pip install pandas -t python/

7.    Zip the contents of the python folder into a layer.zip file by running the following command:

$ zip -r layer.zip python

8.    Publish the Lambda layer by running the following command:

Important: Replace us-east-1 with the AWS Region that your Lambda function is in.

$ aws lambda publish-layer-version --layer-name pandas-layer --zip-file fileb://layer.zip --compatible-runtimes python3.8 --region us-east-1

9.    Add the layer to your Lambda function.