Error while importing python-whois on AWS Lambda

72 views Asked by At

I'm trying to run a script that uses python-whois and I'm having an error. This was previously imported locally using "pip3 install python-whois -t ." and then uploaded to the zip file into AWS Lambda. I'm using Python v3.12

import json
import whois


def lambda_handler(event, context):    
    
    return {
        'statusCode': 200,
        'body': event
    }

The error message:

[ERROR] Runtime.ImportModuleError: Unable to import module 'lambda_function': No module named 'imp'
Traceback (most recent call last):
3

There are 3 answers

0
Andre On BEST ANSWER

I found the answer to my question.

Presently (28 January 2024), the "imp" library is unavailable on AWS Lambda Python v3.12. I moved to v3.11, and it worked just fine.

0
Michelle Luna Alama On

Not sure if your case, but share the way keeping a file structure works for me when using Pillow in AWS Lambda.

  1. Created a virtual environment then installed pillow on it.
  2. Then I moved site-packages directories to my own files structure which look like this:
python/
├── lib/
    ├── python3.12/
        ├── site-packages/
            ├── PIL
            ├── pillow-10.2.0.dist-info
            └── more libraries...
  1. Then I zipped python folder and use the zip as Lambda Layer and it works.

enter image description here

enter image description here

0
Vandalism On

The most efficient and secure way for including dependencies to lambda is either to use EC2 instance or containers (like Docker).

Here's an example for Docker. Create a separate folder containing requirements.txt with your required libraries, in your case whois:

docker run -v "C:/Users/Michelle/Desktop/lambda-layer/whois-layer":/var/task "public.ecr.aws/sam/build-python3.12" /bin/sh -c "pip install -r requirements.txt -t /var/task/python/lib/python3.12/site-packages/; exit"

then zip and upload.