Unable to import modules from aws lambda layer

故事扮演 提交于 2020-06-17 08:06:40

问题


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.

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  

回答1:


If layer would suite you, I can let you know that I just successful constructed a django layer based on your requirements.txt file which could be imported in lambda.

To build the layer I used lambci/lambda tool. To ease the process, I used the following snippet from here.

The layer had 42 MB which is close to the 50 MB limit of lambda.

If you don't want to use layer, you can also use the same docker tool to build regular deployment package as explained in the recent AWS blog:

  • How do I create a Lambda layer using a simulated Lambda environment with Docker?

Since layer works, including django in a regular package should also work.



来源:https://stackoverflow.com/questions/62404733/unable-to-import-modules-from-aws-lambda-layer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!