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

3 minute read
23

I receive an "Unable to import module" error when I try to run my AWS Lambda code in Python.

Short description

You receive an "Unable to import module" error when the AWS Lambda environment can't find the specified library in your Lambda deployment package.

To resolve this error, create a deployment package with all the required libraries. Or, create a Lambda layer with the needed libraries and attach it to your Lambda function. The Lambda layer method allows you to reuse this layer across multiple Lambda functions.

Resolution

Create a Lambda layer to attach to multiple Lambda functions

Note: When you create the Lambda layer, place the libraries in the /python or python/lib/python3.x/site-packages folders. It's a best practice to create the Lambda layer on the same operating system (OS) that your Lambda runtime is based on. For example, Python 3.12 is based on an Amazon Linux 2023 Amazon Machine Image (AMI). So, create the layer on an Amazon Linux 2023 OS.

If your Amazon Elastic Compute Cloud (Amazon EC2) instance doesn't have permissions to upload Lambda layers through the PublishLayerVersion API call, follow steps 1-3. If your instance already has the permissions, then proceed to step 4.

  1. Use the Amazon EC2 console to create an instance with Amazon Linux 2023 AMI. Or, use the AWS Cloud9 console.

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

    {
     "Version": "2012-10-17",
     "Statement": [
     {
     "Sid": "VisualEditor0",
     "Effect": "Allow",
     "Action": "lambda:PublishLayerVersion",
     "Resource": "*"
     }
     ]
    }
  3. Create an IAM role, and then attach the IAM policy to the role.

  4. Attach the IAM role to the instance.

  5. Connect to your instance or the Cloud9 environment.

  6. Run the following commands to create a new folder and use pip to install the library named "numpy":

    mkdir -p lambda-layer/python
    cd lambda-layer/python
    pip3 install --platform manylinux2014_x86_64 --target . --python-version 3.12 --only-binary=:all: numpy

    Note: Update the platform parameter for your function type. For a x86_64 Lambda function, set the value to manylinux2014_x86_64. For an arm64 function, set the value to manylinux2014_aarch64. Update the python-version parameter to the same version that your Lambda function uses.

  7. Run the following command to put the contents of the python folder into a layer.zip file:

    cd ..
    zip -r layer.zip python

    Run the following command to publish the Lambda layer:

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

    Note: Replace us-east-1 with the AWS Region of your Lambda function.

  8. Add the layer to your Lambda function.

  9. To test your Lambda function, import the package and print the version.
    Example of a successful output: 

    import json
    import numpy
    
    
    def lambda_handler(event, context):
     print(numpy.__version__)
     return {
     'statusCode': 200,
     'body': json.dumps('Hello from Lambda!')
     }	
AWS OFFICIAL
AWS OFFICIALUpdated 3 months ago
7 Comments

How can i add multiple python packages to the same layer. Or how to add a python package to an existing layer?

Bharath
replied 9 months ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

profile pictureAWS
MODERATOR
replied 9 months ago

Great content, very informative.

replied 3 months ago

Solved my issue, exactly what I was looking for!

AWS
replied 3 months ago

Very clear instruction, thanks

replied 3 months ago

Python 3.12 is based on an Amazon Linux 2023 Amazon Machine Image (AMI). So, create the layer on an Amazon Linux 2023 OS.

Yeah, ok, but default python on this image is 3.9. I can only update to python 3.11 using dnf - so, feels like you left some quite important steps out here?

akpt
replied 3 months ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

profile pictureAWS
MODERATOR
replied 3 months ago