如何创建任何 AWS Lambda Python 层? (XGBoost 的使用示例)
How to create any AWS Lambda Python Layer? (Usage example with XGBoost)
我在为 xgboost 库创建 lambda 层时遇到问题。我运行:
我从这里 (https://github.com/alexeybutyrev/aws_lambda_xgboost) 抓取了一个 xgboost 的 zip 及其依赖项,并将其加载到一个层中。当我尝试测试我的 lambda 时,出现此错误:
Unable to import module 'lambda_function': No module named 'xgboost.core'
看起来 __init__.py
正试图通过 from .core import <stuff>
引用 core.py
有人在使用 AWS Lambda 之前遇到过这个错误吗?
所以我一直无法弄清楚为什么它会以这种方式失败。我发现有效的解决方案是创建一个 EC2 实例 运行 amazon linux,在那里安装并压缩库,然后保存到 S3。有关详细说明,请参阅此处:
https://medium.com/@lucashenriquessilva/how-to-create-a-aws-lambda-python-layer-db2830e08b12
编辑: 正如@Marcin 所说,提供的第一个答案适用于 262 MB 以下的大包。
一个。 Python Lambda 层大小限制内的包
您也可以使用 AWS sam cli 和 Docker(请参阅 link 安装 SAM cli),在容器内构建包。基本上,您使用 Python
初始化默认模板作为运行时,然后在 requirements.txt
文件下指定包。我发现它比你提到的文章更容易。如果您想考虑将来使用它们,我会告诉您步骤。
1。初始化默认 SAM 模板
在您要保留项目的任何文件夹下,您可以键入
sam init
这将提示一系列问题,为了快速设置,我们将选择快速启动模板,如下所示
1 - AWS Quick Start Templates
2 - Python 3.8
Project name [sam-app]: your_project_name
1 - Hello World Example
通过选择 Hello World Example
,它会生成一个带有 requirements.txt
文件的默认 lambda function
。现在,我们将使用您想要的包的名称进行编辑,在本例中为 xgboost
2。指定要安装的包
cd your_project_name
code hello_world/requirements.txt
因为我有 Visual Studio 作为编辑器的代码,这将打开文件。现在,我可以指定 xgboost
包
your_python_package
这是安装 Docker 的原因。一些软件包依赖于 C++
。因此,建议在容器内构建(Windows 上的案例)。现在,移动到 template.yaml
文件所在的文件夹。然后,输入
sam build -u
3。压缩包
有些文件您不想包含在您的 lambda 层中,因为我们只想保留 python 库。因此,您可以删除以下文件
rm .aws-sam/build/HelloWorldFunction/app.py
rm .aws-sam/build/HelloWorldFunction/__init__.py
rm .aws-sam/build/HelloWorldFunction/requirements.txt
然后压缩文件夹的剩余内容。
cp -r .aws-sam/build/HelloWorldFunction/ python/
zip -r my_layer.zip python/
我们根据docs将层放在python/
文件夹中
在 Windows 系统上, zip
命令应替换为
Compress-Archive my_layer/ my_layer.zip.
4。将您的层上传到 AWS
在 AWS 上转到 Lambda
,然后选择 Layers
和 Create Layer
。现在,您可以上传 .zip
文件,如下图所示
请注意,对于超过 50 MB 的 zip 文件,您应该将 .zip
文件上传到 s3 存储桶并提供路径,例如 https://s3:amazonaws.com//mybucket/my_layer.zip
.
乙。 Python 个超过 Lambda 层限制的包
xgboost
包本身超过 300 MB,将抛出以下错误
正如@Marcin 所指出的那样,之前使用 SAM cli 的方法不能直接用于超过限制的 Python 层。在 github 上有一个未解决的问题,用于在 运行 sam build -u
时指定自定义 docker 图像,以及重新标记默认 lambda/lambci
图像的可能解决方案。
那么,我们怎么才能通过呢?。我只想指出一些有用的资源。
- 首先,Medium article that @Alex took as solution that follow this repo code.
- 二、alexeybutyrev approach that works by applying the strip command to reduce the libraries sizes. One can find this approach under a github repo,提供使用说明
编辑(2020 年 12 月)
本月 AWS 发布 container Image 对 AWS Lambda 的支持。按照项目的下一个树结构
Project/
|-- app/
| |-- app.py
| |-- requirements.txt
| |-- xgb_trained.bin
|-- Dockerfile
您可以使用以下 Docker 图像部署 XGBoost 模型。请按照此 repo 说明进行详细说明。
# Dockerfile based on https://docs.aws.amazon.com/lambda/latest/dg/images-create.html
# Define global args
ARG FUNCTION_DIR="/function"
ARG RUNTIME_VERSION="3.6"
# Choose buster image
FROM python:${RUNTIME_VERSION}-buster as base-image
# Install aws-lambda-cpp build dependencies
RUN apt-get update && \
apt-get install -y \
g++ \
make \
cmake \
unzip \
libcurl4-openssl-dev \
git
# Include global arg in this stage of the build
ARG FUNCTION_DIR
# Create function directory
RUN mkdir -p ${FUNCTION_DIR}
# Copy function code
COPY app/* ${FUNCTION_DIR}/
# Install python dependencies and runtime interface client
RUN python${RUNTIME_VERSION} -m pip install \
--target ${FUNCTION_DIR} \
--no-cache-dir \
awslambdaric \
-r ${FUNCTION_DIR}/requirements.txt
# Install xgboost from source
RUN git clone --recursive https://github.com/dmlc/xgboost
RUN cd xgboost; make -j4; cd python-package; python${RUNTIME_VERSION} setup.py install; cd;
# Multi-stage build: grab a fresh copy of the base image
FROM base-image
# Include global arg in this stage of the build
ARG FUNCTION_DIR
# Set working directory to function root directory
WORKDIR ${FUNCTION_DIR}
# Copy in the build image dependencies
COPY --from=base-image ${FUNCTION_DIR} ${FUNCTION_DIR}
ENTRYPOINT [ "/usr/local/bin/python", "-m", "awslambdaric" ]
CMD [ "app.handler" ]
我在为 xgboost 库创建 lambda 层时遇到问题。我运行:
我从这里 (https://github.com/alexeybutyrev/aws_lambda_xgboost) 抓取了一个 xgboost 的 zip 及其依赖项,并将其加载到一个层中。当我尝试测试我的 lambda 时,出现此错误:
Unable to import module 'lambda_function': No module named 'xgboost.core'
看起来 __init__.py
正试图通过 from .core import <stuff>
有人在使用 AWS Lambda 之前遇到过这个错误吗?
所以我一直无法弄清楚为什么它会以这种方式失败。我发现有效的解决方案是创建一个 EC2 实例 运行 amazon linux,在那里安装并压缩库,然后保存到 S3。有关详细说明,请参阅此处:
https://medium.com/@lucashenriquessilva/how-to-create-a-aws-lambda-python-layer-db2830e08b12
编辑: 正如@Marcin 所说,提供的第一个答案适用于 262 MB 以下的大包。
一个。 Python Lambda 层大小限制内的包
您也可以使用 AWS sam cli 和 Docker(请参阅 link 安装 SAM cli),在容器内构建包。基本上,您使用 Python
初始化默认模板作为运行时,然后在 requirements.txt
文件下指定包。我发现它比你提到的文章更容易。如果您想考虑将来使用它们,我会告诉您步骤。
1。初始化默认 SAM 模板
在您要保留项目的任何文件夹下,您可以键入
sam init
这将提示一系列问题,为了快速设置,我们将选择快速启动模板,如下所示
1 - AWS Quick Start Templates
2 - Python 3.8
Project name [sam-app]: your_project_name
1 - Hello World Example
通过选择 Hello World Example
,它会生成一个带有 requirements.txt
文件的默认 lambda function
。现在,我们将使用您想要的包的名称进行编辑,在本例中为 xgboost
2。指定要安装的包
cd your_project_name
code hello_world/requirements.txt
因为我有 Visual Studio 作为编辑器的代码,这将打开文件。现在,我可以指定 xgboost
包
your_python_package
这是安装 Docker 的原因。一些软件包依赖于 C++
。因此,建议在容器内构建(Windows 上的案例)。现在,移动到 template.yaml
文件所在的文件夹。然后,输入
sam build -u
3。压缩包
有些文件您不想包含在您的 lambda 层中,因为我们只想保留 python 库。因此,您可以删除以下文件
rm .aws-sam/build/HelloWorldFunction/app.py
rm .aws-sam/build/HelloWorldFunction/__init__.py
rm .aws-sam/build/HelloWorldFunction/requirements.txt
然后压缩文件夹的剩余内容。
cp -r .aws-sam/build/HelloWorldFunction/ python/
zip -r my_layer.zip python/
我们根据docs将层放在python/
文件夹中
在 Windows 系统上, zip
命令应替换为
Compress-Archive my_layer/ my_layer.zip.
4。将您的层上传到 AWS
在 AWS 上转到 Lambda
,然后选择 Layers
和 Create Layer
。现在,您可以上传 .zip
文件,如下图所示
请注意,对于超过 50 MB 的 zip 文件,您应该将 .zip
文件上传到 s3 存储桶并提供路径,例如 https://s3:amazonaws.com//mybucket/my_layer.zip
.
乙。 Python 个超过 Lambda 层限制的包
xgboost
包本身超过 300 MB,将抛出以下错误
正如@Marcin 所指出的那样,之前使用 SAM cli 的方法不能直接用于超过限制的 Python 层。在 github 上有一个未解决的问题,用于在 运行 sam build -u
时指定自定义 docker 图像,以及重新标记默认 lambda/lambci
图像的可能解决方案。
那么,我们怎么才能通过呢?。我只想指出一些有用的资源。
- 首先,Medium article that @Alex took as solution that follow this repo code.
- 二、alexeybutyrev approach that works by applying the strip command to reduce the libraries sizes. One can find this approach under a github repo,提供使用说明
编辑(2020 年 12 月)
本月 AWS 发布 container Image 对 AWS Lambda 的支持。按照项目的下一个树结构
Project/
|-- app/
| |-- app.py
| |-- requirements.txt
| |-- xgb_trained.bin
|-- Dockerfile
您可以使用以下 Docker 图像部署 XGBoost 模型。请按照此 repo 说明进行详细说明。
# Dockerfile based on https://docs.aws.amazon.com/lambda/latest/dg/images-create.html
# Define global args
ARG FUNCTION_DIR="/function"
ARG RUNTIME_VERSION="3.6"
# Choose buster image
FROM python:${RUNTIME_VERSION}-buster as base-image
# Install aws-lambda-cpp build dependencies
RUN apt-get update && \
apt-get install -y \
g++ \
make \
cmake \
unzip \
libcurl4-openssl-dev \
git
# Include global arg in this stage of the build
ARG FUNCTION_DIR
# Create function directory
RUN mkdir -p ${FUNCTION_DIR}
# Copy function code
COPY app/* ${FUNCTION_DIR}/
# Install python dependencies and runtime interface client
RUN python${RUNTIME_VERSION} -m pip install \
--target ${FUNCTION_DIR} \
--no-cache-dir \
awslambdaric \
-r ${FUNCTION_DIR}/requirements.txt
# Install xgboost from source
RUN git clone --recursive https://github.com/dmlc/xgboost
RUN cd xgboost; make -j4; cd python-package; python${RUNTIME_VERSION} setup.py install; cd;
# Multi-stage build: grab a fresh copy of the base image
FROM base-image
# Include global arg in this stage of the build
ARG FUNCTION_DIR
# Set working directory to function root directory
WORKDIR ${FUNCTION_DIR}
# Copy in the build image dependencies
COPY --from=base-image ${FUNCTION_DIR} ${FUNCTION_DIR}
ENTRYPOINT [ "/usr/local/bin/python", "-m", "awslambdaric" ]
CMD [ "app.handler" ]