5

I've had an absolute terrible time trying to get the Snowflake python connector to install. That is a side frustration in this, but i've finally managed to get it to install by using a full Ubuntu base Docker image. I cannot figure out how to make the AWS lambda wrappers work now though.

Project structure.

├── 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}"

Command

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

And the errors:

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
pyFiddler
  • 301
  • 3
  • 11
  • Run the docker in interactive mode, check if the python is there, path error will cause problem, secondly, awslamdaric will try to find "python", not "python3" on path, so you need to clone the binary and chmod +x – Trung Đức Jul 07 '22 at 07:13

1 Answers1

0

use this entry file:

#!/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

NOTE: I removed the path to python3

Elia Weiss
  • 8,324
  • 13
  • 70
  • 110