执行Lambda python函数时出现模块导入错误

时间:2018-03-21 05:54:44

标签: python python-2.7 aws-lambda

我正在尝试在AWS Lambda上执行python函数。在我的函数中,我试图导入mysql.connector模块。但是错误正在迸发:

  

errorMessage“:”没有名为'mysql.connector'的模块“

我最初在我的EC2实例中编写了我的python代码。我使用pip在我的python文件目录中安装了mysql-connector。

pip install mysql-connector -t /path/to/file/dir

我上传了唯一文件的zip文件,而不是任何包含该文件的文件夹。

3 个答案:

答案 0 :(得分:2)

Pattern that I have been using to deploy Python libs to lambda is following

Firstly, prior to packaging lambda function install all of the requirements into $SOURCE_ROOT/lib folder

pip install -r requirements.txt -t ./lib

Secondly, dd automatic import of this folder in your lambda entrypoint (that is lambda handler)

import os
import sys

# if running in lambda
if 'LAMBDA_TASK_ROOT' in os.environ:
  sys.path.append(f"{os.environ['LAMBDA_TASK_ROOT']}/lib")

# this will render all of your packages placed as subdirs available
sys.path.append(os.path.dirname(os.path.realpath(__file__)))

Extending sys.path with your own packaged path is the crucial for this to work.

Note on native compiled extensions

Note that if you are packaging any natively compiled extensions, their compilation should be done on linux-compatible O/S, ideally on EC2 instance created from Amazon Linux AMIs Lambda is running from (currently amzn-ami-hvm-2017.03.1.20170812-x86_64-gp2, but up to date information can be always obtained from Amazon Official Docs. According to my experience, extensions built from official Docker python container worked on lambda, without need to compile on actual EC2 instance created from AMIs, but there is no guarantee as offical docs state that

If you are using any native binaries in your code, make sure they are compiled in this environment. Note that only 64-bit binaries are supported on AWS Lambda.

MySQL Connector

Quick look at MySQL connector for Python gives impression that by default package will use native Python implementation, so no C extension is loaded

答案 1 :(得分:0)

Lambda就像一个只安装了python的EC2实例。您需要包含使用部署包本身运行python代码所需的所有包。运行代码时,不会预先安装任何软件包。

答案 2 :(得分:0)

有两种方法可以将Python依赖项与Lambda打包在一起。

  1. 将其与函数本身打包在一起。为此,您只需转到函数的根文件夹,然后将依赖项安装在该文件夹本身中,而不要安装到其他任何文件夹中。
$ pip install -t <some-package> .

然后从根目录压缩该文件夹并上传,您的zip看起来应该像这样:

.
├── lambda_function.py
├── pymysql
│   └── ...
└── PyMySQL-0.9.3.dist-info
    └── ...
  1. 第二种方法是我一直遵循的标准,我从不随带lambda函数运送库或外部包,而是始终创建Layers。
  

图层是一个ZIP归档文件,其中包含库,自定义运行时或   其他依赖项。通过图层,您可以在自己的库中使用库   功能,而无需将其包含在部署包中。

docs

中了解有关Lambda图层的更多信息