DEV Community

Discussion on: Deploy Python Lambda Functions With Container Image

Collapse
 
chachan profile image
Cherny

you should use aws-lambda-rie (emulator) instead of aws-lambda-ric - docs.aws.amazon.com/lambda/latest/.... I believe that environment variable is set by AWS, so if it's not present that script will execute the emulator. This is an example

entry_script.sh

#!/bin/sh

if [ -z "${AWS_LAMBDA_RUNTIME_API}" ]; then
    exec /usr/local/bin/aws-lambda-rie /usr/local/bin/python -m awslambdaric $@
else
    exec /usr/local/bin/python -m awslambdaric $@
fi

Enter fullscreen mode Exit fullscreen mode

Dockerfile

FROM python:3.9-bullseye

ARG FUNCTION_DIR="/function"
WORKDIR ${FUNCTION_DIR}

RUN curl -Lo /usr/local/bin/aws-lambda-rie \
    https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie && \
    chmod +x /usr/local/bin/aws-lambda-rie

COPY entry_script.sh ${FUNCTION_DIR}

# Install dependencies
RUN pip install \
    --target ${FUNCTION_DIR} \
    awslambdaric

# Copy function code
COPY src/* ${FUNCTION_DIR}

ENTRYPOINT [ "sh", "entry_script.sh" ]
CMD [ "app.handler" ]
Enter fullscreen mode Exit fullscreen mode