Lambda Docker镜像无法运行

5
我曾经试图安装Snowflake python连接器,但是一直遇到困难。虽然这是一个小小的挫折,但最终我通过使用完整的Ubuntu基础Docker镜像成功安装了它。现在我无法弄清楚如何让AWS lambda包装器正常工作。
项目结构。
├── Dockerfile
├── app
│   ├── __init__.py
│   └── app.py
└── entry.sh

entry.sh

#!/bin/sh
if [ -z "${AWS_LAMBDA_RUNTIME_API}" ]; then

    exec /usr/bin/aws-lambda-rie /usr/local/bin/python3 -m awslambdaric $1
else
    exec /usr/local/bin/python3 -m awslambdaric $1
fi

Dockerfile
ARG FUNCTION_DIR="/home/app/"

FROM ubuntu:20.04
RUN apt-get update && apt-get install -y \
    python3 \
    python3-pip
ARG FUNCTION_DIR
RUN mkdir -p ${FUNCTION_DIR}
RUN pip install -U wheel pip --target ${FUNCTION_DIR}
RUN pip install snowflake-connector-python==2.7.1 --target ${FUNCTION_DIR}
RUN pip install awslambdaric --target ${FUNCTION_DIR}
WORKDIR ${FUNCTION_DIR}
ADD https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie /usr/bin/aws-lambda-rie
COPY app/* ${FUNCTION_DIR}
COPY entry.sh /
RUN chmod 755 /usr/bin/aws-lambda-rie /entry.sh
ENTRYPOINT [ "/entry.sh" ]
CMD [ "app.handler" ]

app.py

import sys
import snowflake.connector

def handler(event, context):
    print(dir(snowflake.connector.connect))
    print(f"\nEvent: {event}\n")
    print(f"\nContext: {context}\n")
    return f"Hello world! {sys.version}"

命令

curl -X POST \
   "localhost:8080/2015-03-31/functions/function/invocations" \
   -d '{"foo": "bar"}'

并且还有错误信息:

10 Dec 2021 06:31:09,383 [INFO] (rapid) exec '/usr/local/bin/python3' (cwd=/home/app, handler=app.handler)
10 Dec 2021 06:31:22,860 [INFO] (rapid) extensionsDisabledByLayer(/opt/disable-extensions-jwigqn8j) -> stat /opt/disable-extensions-jwigqn8j: no such file or directory
10 Dec 2021 06:31:22,860 [WARNING] (rapid) Cannot list external agents error=open /opt/extensions: no such file or directory
START RequestId: 57f0b49d-f2b3-4f25-96fa-db287d01ca62 Version: $LATEST
10 Dec 2021 06:31:22,860 [WARNING] (rapid) First fatal error stored in appctx: Runtime.InvalidEntrypoint
10 Dec 2021 06:31:22,860 [ERROR] (rapid) Init failed error=fork/exec /usr/local/bin/python3: no such file or directory InvokeID=
10 Dec 2021 06:31:22,860 [WARNING] (rapid) Reset initiated: ReserveFail
10 Dec 2021 06:31:22,861 [WARNING] (rapid) Cannot list external agents error=open /opt/extensions: no such file or directory
10 Dec 2021 06:31:22,861 [WARNING] (rapid) First fatal error stored in appctx: Runtime.InvalidEntrypoint

以交互模式运行Docker,检查Python是否存在,路径错误会导致问题。其次,awslamdaric将尝试在路径上找到“python”,而不是“python3”,因此您需要克隆二进制文件并chmod +x。 - Trung Đức
1个回答

0
请使用此入口文件:
#!/bin/sh
if [ -z "${AWS_LAMBDA_RUNTIME_API}" ]; then
    exec /usr/bin/aws-lambda-rie python3 -m awslambdaric $1
else
    exec python3 -m awslambdaric $1
fi

注意:我已删除到python3的路径


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接