AWS Lambda is Unable to find app.handler (Custom Docker Image)

  amazon-web-services, aws-lambda, docker, python-3.x

I want to deploy machine learning related code on AWS Lambda function as Docker image. Base Images provided by AWS for Python, don’t allow to install using apt-get command. So I created custom docker image for AWS Lambda. Below is the code of my Dockerfile.

ref: Custom Image Documentation by AWS

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" ]

My Folder structure is as below:

aws_lambda (Folder)
- Dockerfile
- app.py
- function (Folder)
  - app.py
- requirements.txt

When I run docker image, it shows error as below:

[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'

It seems it is unable to find /function directory but it is already there.

Source: Docker Questions

LEAVE A COMMENT