【发布时间】:2021-05-27 20:29:49
【问题描述】:
我想在 AWS Lambda 函数上部署机器学习相关代码作为 Docker 映像。 AWS 为 Python 提供的基础镜像,不允许使用 apt-get 命令安装。所以我为 AWS Lambda 创建了自定义 docker 镜像。下面是我的 Dockerfile 的代码。
参考:Create an image from an alternative base image
ARG FUNCTION_DIR="/function"
FROM python:3.7-buster as build-image
# Install aws-lambda-cpp build dependencies
RUN apt-get update && \
apt-get install -y \
g++ \
make \
cmake \
unzip \
libcurl4-openssl-dev
RUN apt-get install -y python-opencv
# Include global arg in this stage of the build
ARG FUNCTION_DIR
# Create function directory
RUN mkdir -p ${FUNCTION_DIR}
# Install the runtime interface client
RUN pip install \
--target ${FUNCTION_DIR} \
awslambdaric
FROM python:3.7-buster
ARG FUNCTION_DIR
# Set working directory to function root directory
WORKDIR ${FUNCTION_DIR}
# Copy in the build image dependencies
COPY --from=build-image ${FUNCTION_DIR} ${FUNCTION_DIR}
COPY requirements.txt /
#RUN pip install -r /requirements.txt
COPY . /function
ENV AWS_LAMBDA_RUNTIME_API=python3.7
ENTRYPOINT [ "/usr/local/bin/python", "-m", "awslambdaric" ]
CMD [ "app.handler" ]
我的文件夹结构如下:
aws_lambda (Folder)
- Dockerfile
- app.py
- function (Folder)
- app.py
- requirements.txt
当我运行 docker image 时,它显示如下错误:
[ERROR] [1614258613176] LAMBDA_RUNTIME Failed to get next invocation. No Response from endpoint
Traceback (most recent call last):
File "/usr/local/lib/python3.7/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/usr/local/lib/python3.7/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/function/awslambdaric/__main__.py", line 21, in <module>
main(sys.argv)
File "/function/awslambdaric/__main__.py", line 17, in main
bootstrap.run(app_root, handler, lambda_runtime_api_addr)
File "/function/awslambdaric/bootstrap.py", line 416, in run
event_request = lambda_runtime_client.wait_next_invocation()
File "/function/awslambdaric/lambda_runtime_client.py", line 76, in wait_next_invocation
response_body, headers = runtime_client.next()
RuntimeError: Failed to get next
Executing 'app.handler' in function directory '/function'
似乎找不到/function目录,但它已经在那里了。
【问题讨论】:
-
在我看来,错误出在其他地方。你在哪里运行容器?是本地测试还是你已经部署?
-
本地工作正常。仅在部署时不起作用。
-
您是否尝试过在本地使用运行时接口仿真器 (RIE)?它似乎不是从 dockerfile 安装的。使用 RIE 进行测试为您提供了与 lambda 相同的环境,因此您可以准确调试正在发生的事情。此外,您为什么要设置 AWS_LAMBDA_RUNTIME_API ?由于您使用的是 python 运行时接口客户端,因此不需要它并由 lambda 自动设置。
标签: python-3.x amazon-web-services docker aws-lambda