[英]AWS Lambda Code Unable to Import Depdendencies from S3 — Runtime.ImportModuleError
I'm trying to deploy a Python lambda function with external dependencies, but I'm hitting an error because it doesn't see my external dependencies. 我正在尝试使用外部依赖项部署Python lambda函数,但我遇到了错误,因为它没有看到我的外部依赖项。
"Unable to import module 'metrics': No module named 'github'"
Here's my deploy script. 这是我的部署脚本。 My python script with the lambda_handler() is
metrics.py
. 我的lambda_handler()的python脚本是
metrics.py
。
mkdir lambda_code
# populate lambda_code directory with python libraries
pip3 install --quiet -r requirements.txt --target lambda_code/
# compress the lambda_code directory and add metrics.py to the zip
zip -qq -r9 lambda_code.zip lambda_code/
zip -qq -g lambda_code.zip metrics.py
aws s3 cp lambda_code.zip s3://$BUCKET/lambda_code.zip
aws lambda update-function-code --function-name $FUNCTION_NAME --s3-bucket $BUCKET --s3-key lambda_code.zip
Here's the tree of my upacked lamdba_code.zip This is where things aren't working. 这是我的upacked lamdba_code.zip的树, 这是事情不起作用的地方。 It doesn't make sense to me why the lambda can't see the github module.
我没理由为什么lambda看不到github模块。 I've also tried putting
metrics.py
directly in the lambda_code directory, but still nothing. 我也尝试将
metrics.py
直接放在lambda_code目录中,但仍然没有。
.
├── lambda_code
│ ├── Deprecated-1.2.5.dist-info
│ ├── PyGithub-1.43.7.dist-info
│ ├── PyJWT-1.7.1.dist-info
│ ├── __pycache__
│ ├── bin
│ ├── certifi
│ ├── certifi-2019.3.9.dist-info
│ ├── chardet
│ ├── chardet-3.0.4.dist-info
│ ├── cycler-0.10.0.dist-info
│ ├── cycler.py
│ ├── dateutil
│ ├── deprecated
│ ├── easy_install.py
│ ├── github
│ ├── idna
│ ├── idna-2.8.dist-info
│ ├── jwt
│ ├── kiwisolver-1.1.0.dist-info
│ ├── kiwisolver.cpython-37m-darwin.so
│ ├── matplotlib
│ ├── matplotlib-3.0.3-py3.7-nspkg.pth
│ ├── matplotlib-3.0.3.dist-info
│ ├── mpl_toolkits
│ ├── numpy
│ ├── numpy-1.16.3.dist-info
│ ├── pandas
│ ├── pandas-0.24.2.dist-info
│ ├── pkg_resources
│ ├── pylab.py
│ ├── pyparsing-2.4.0.dist-info
│ ├── pyparsing.py
│ ├── python_dateutil-2.8.0.dist-info
│ ├── pytz
│ ├── pytz-2019.1.dist-info
│ ├── requests
│ ├── requests-2.21.0.dist-info
│ ├── setuptools
│ ├── setuptools-41.0.1.dist-info
│ ├── six-1.12.0.dist-info
│ ├── six.py
│ ├── urllib3
│ ├── urllib3-1.24.3.dist-info
│ ├── wrapt
│ └── wrapt-1.11.1.dist-info
└── metrics.py
Finally, here's the beginning of the lambda code. 最后,这是lambda代码的开头。 The error occurs when trying to import github.
尝试导入github时发生错误。
"""Obtains total number of releases on Github.com and creates data
visualizations"""
import datetime
import io
import os
import sys
from base64 import b64decode
from github import Github
import boto3
import matplotlib.pyplot as plt
import pandas as pd
ENCRYPTED = os.environ['github_credentials']
DECRYPTED =
boto3.client('kms').decrypt(CiphertextBlob=b64decode(ENCRYPTED)) .
['Plaintext']
def lambda_handler(event, context):
You either need to have your metrics.py
and the sub folders of lambda_code
into a single folder, or you need to import the modules like, lambda_code.pytz
for each module that is zipped. 你要么需要有你
metrics.py
和子文件夹lambda_code
到一个文件夹,或者您需要导入像,模块lambda_code.pytz
对于拉上每个模块。
If you follow your current directory structure, lambda_code
becomes a module and all other modules can be referred only using lambda_code.
如果您遵循当前的目录结构,
lambda_code
将成为一个模块,并且所有其他模块只能使用lambda_code.
because, folders inside lambda_code
becomes sub modules of lambda_code
. 因为,里面的文件夹
lambda_code
成为子模块lambda_code
。 I would suggest you to copy sub folders of lambda_code
to root directory, ie the directory which your metrics.py
resides. 我建议你将
lambda_code
子文件夹复制到根目录,即metrics.py
所在的目录。 Then delete the lambda_code
folder and zip and upload. 然后删除
lambda_code
文件夹并压缩并上传。 This way, you might not need to edit your code. 这样,您可能不需要编辑代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.