0

I am trying to build a docker image which will be deployed as a function on AWS Lambda. Able to build and test the image successfully but facing an issue when I try to import OpenCV in the function.

I do not face this issue when I remove the import statement from app.py

The error I am facing -

{"errorMessage": "Unable to import module 'app': libGL.so.1: cannot open shared object file: No such file or directory", "errorType": "Runtime.ImportModuleError"}

My Dockerfile -

# Define custom function directory
ARG FUNCTION_DIR="/function"

FROM python:3.9 as build-image

# Include global arg in this stage of the build
ARG FUNCTION_DIR

# 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 --fix-missing \
    build-essential \
    cmake \
    gfortran \
    git \
    wget \
    curl \
    graphicsmagick \
    libgraphicsmagick1-dev \
    libatlas-base-dev \
    libavcodec-dev \
    libavformat-dev \
    libgtk2.0-dev \
    libjpeg-dev \
    liblapack-dev \
    libswscale-dev \
    pkg-config \
    python3-dev \
    python3-numpy \
    software-properties-common \
    zip \
    && apt-get clean && rm -rf /tmp/* /var/tmp/*

# Copy function code
RUN mkdir -p ${FUNCTION_DIR}
COPY app/* ${FUNCTION_DIR}

WORKDIR ${FUNCTION_DIR}

RUN pip install -r requirements.txt --target ${FUNCTION_DIR}

# Install the function's dependencies
RUN pip install \
    --target ${FUNCTION_DIR} \
        awslambdaric


FROM python:3.9

# Include global arg in this stage of the build
ARG FUNCTION_DIR
# Set working directory to function root directory
WORKDIR ${FUNCTION_DIR}

# Copy in the built dependencies
COPY --from=build-image ${FUNCTION_DIR} ${FUNCTION_DIR}

ENTRYPOINT [ "/usr/local/bin/python", "-m", "awslambdaric" ]
CMD [ "app.handler" ]

My requirements.txt -

mediapipe<=0.8.3.1
numpy<=1.19.4
opencv-python<=4.4.0.46
boto3<=1.17.64

My app.py

import cv2

def handler(event, context):
    return cv2.__version__
Varun
  • 17
  • 7
  • Related: https://stackoverflow.com/questions/64016819/cant-use-opencv-python-in-aws-lambda – jarmod May 11 '21 at 12:40
  • Not quite related. In that post they’re installing it in a lambda layer it seems. – Varun May 11 '21 at 18:37
  • They include steps to install and locate libGL.so.1, which is what you are missing. – jarmod May 11 '21 at 21:19
  • Oh yes you're right, managed to solve it using the solution provided in this question - [Stackoverflow question](https://stackoverflow.com/questions/55313610/importerror-libgl-so-1-cannot-open-shared-object-file-no-such-file-or-directo) – Varun May 12 '21 at 09:36

1 Answers1

0

Managed to solve it by adding these to the Dockerfile -

RUN apt-get install ffmpeg libsm6 libxext6  -y

New Dockerfile looks like this -

# Define custom function directory
ARG FUNCTION_DIR="/function"

FROM python:3.9 as build-image

# Include global arg in this stage of the build
ARG FUNCTION_DIR

# 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 --fix-missing \
    build-essential \
    cmake \
    gfortran \
    git \
    wget \
    curl \
    ffmpeg \ 
    libsm6 \
    libxext6 \
    graphicsmagick \
    libgraphicsmagick1-dev \
    libatlas-base-dev \
    libavcodec-dev \
    libavformat-dev \
    libgtk2.0-dev \
    libjpeg-dev \
    liblapack-dev \
    libswscale-dev \
    pkg-config \
    python3-dev \
    python3-numpy \
    software-properties-common \
    zip \
    && apt-get clean && rm -rf /tmp/* /var/tmp/*

# Copy function code
RUN mkdir -p ${FUNCTION_DIR}
COPY app/* ${FUNCTION_DIR}

WORKDIR ${FUNCTION_DIR}

RUN pip install -r requirements.txt --target ${FUNCTION_DIR}

# Install the function's dependencies
RUN pip install \
    --target ${FUNCTION_DIR} \
        awslambdaric


FROM python:3.9

# Include global arg in this stage of the build
ARG FUNCTION_DIR
# Set working directory to function root directory
WORKDIR ${FUNCTION_DIR}

# Copy in the built dependencies
COPY --from=build-image ${FUNCTION_DIR} ${FUNCTION_DIR}

ENTRYPOINT [ "/usr/local/bin/python", "-m", "awslambdaric" ]
CMD [ "app.handler" ]
Varun
  • 17
  • 7