Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to import modules from aws lambda layer

I have a requirements file for my pip packages. I installed it in a target folder and zipped the contents and uploaded it on AWS lambda layer.

Requirements.txt

asgiref==3.2.3
certifi==2019.11.28
chardet==3.0.4
cloudevents==0.2.4
decorator==4.4.1
Django==3.0
idna==2.8
jaeger-client==4.2.0
jsonpath-ng==1.4.3
pbr==5.4.4
ply==3.11
pytz==2019.3
requests==2.22.0
six==1.13.0
sqlparse==0.3.0
urllib3==1.25.7
aws-xray-sdk
mysql-connector-python
gunicorn

I used the command pip3 install -r requirements.txt -t python/ where python is the target directory. This image shows the content of python directory. enter image description here

After this I zipped the content of this directory and published it as a layer on aws lambda. I made a dummy lambda function to check if the layer is working or not.

import json
import django

def lambda_handler(event, context):
    # TODO implement
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

The log file error :

Response:
{
  "errorMessage": "Unable to import module 'lambda_function'"
}

Request ID:
"66ce85e0-59f0-4e5c-98f9-6fa3e3eb17d0"

Function Logs:
START RequestId: 66ce85e0-59f0-4e5c-98f9-6fa3e3eb17d0 Version: $LATEST
Unable to import module 'lambda_function': No module named 'django'

END RequestId: 66ce85e0-59f0-4e5c-98f9-6fa3e3eb17d0
REPORT RequestId: 66ce85e0-59f0-4e5c-98f9-6fa3e3eb17d0  Duration: 0.64 ms   Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 42 MB  Init Duration: 1.81 ms  
like image 647
Adi shukla Avatar asked Jul 22 '26 04:07

Adi shukla


1 Answers

You mention that you 'zipped the content of this directory' referring to the python folder. When you create a layer via a .zip package, python packages need to be in a 'python' folder subdirectory. They can not be all at the root of the zipped folder.

like image 170
Ryan Lutz Avatar answered Jul 24 '26 17:07

Ryan Lutz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!