serverless / serverless-python-requirements

⚡️🐍📦 Serverless plugin to bundle Python packages

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use custom python docker image with all dependencies included in the aws lambda

Aarif1430 opened this issue · comments

I want to use the docker image in serverless.yml like

  • serverless.yml
custom:
  pythonRequirements:
    slim: true
    dockerizePip: true
    dockerImage: *********.dkr.ecr.us-east-2.amazonaws.com/docker_custom_python:latest
  • Example Docker file
FROM lambci/lambda:python3.8

FROM lambci/lambda-base-2:build

ENV PATH=/var/lang/bin:$PATH \
    LD_LIBRARY_PATH=/var/lang/lib:$LD_LIBRARY_PATH \
    AWS_EXECUTION_ENV=AWS_Lambda_python3.8 \
    PKG_CONFIG_PATH=/var/lang/lib/pkgconfig:/usr/lib64/pkgconfig:/usr/share/pkgconfig \
    PIPX_BIN_DIR=/var/lang/bin \
    PIPX_HOME=/var/lang/pipx

COPY --from=0 /var/runtime /var/runtime
COPY --from=0 /var/lang /var/lang
COPY --from=0 /var/rapid /var/rapid

# Add these as a separate layer as they get updated frequently
RUN pip install -U pip setuptools wheel --no-cache-dir && \
  pip install pipx --no-cache-dir && \
  pipx install virtualenv && \
  pipx install pipenv && \
  pipx install poetry==1.1.4 && \
  pipx install awscli==1.* && \
  pipx install aws-lambda-builders==1.2.0 && \
  pipx install aws-sam-cli==1.15.0

The image should contain all the dependencies like pandas, NumPy, etc. How can I build a docker image like this and use it for all of my lambda functions? Any help in this is much appreciated.

  • Note: I don't want to containerize the function into the docker image and built it every time. Instead, I want to have a single docker image with all the dependencies.