1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Container based AWS python Lambda function cold start time

Discussão em 'Python' iniciado por Stack, Setembro 30, 2024.

  1. Stack

    Stack Membro Participativo

    I've converted a python and R containerized lambda to a multistage build which reduced the size of the container in ECR from 477 MB to 160 MB, but the cold start time is about the same for both (in fact a bit slower for the smaller package (from 2.3 -> 2.97s)

    GIVEN my Dockerfile looks like this:

    # Stage 1: Build stage
    FROM python:3.10-slim-bullseye as builder

    ENV DEBIAN_FRONTEND=noninteractive

    # Set up environment variables
    ENV LC_ALL=C.UTF-8
    ENV LANG=en_US.UTF-8
    ENV TZ=:/etc/localtime
    ENV PATH=/var/lang/bin:/usr/local/bin:/usr/bin/:/bin:/opt/bin
    ENV LD_LIBRARY_PATH=/var/lang/lib:/lib64:/usr/lib64:/var/runtime:/var/runtime/lib:/var/task:/var/task/lib:/opt/lib

    # Install build dependencies and R in build stage
    RUN apt-get update && \
    apt-get install -y \
    g++ \
    make \
    cmake \
    unzip \
    libcurl4-openssl-dev \
    r-base-core \
    && apt-get clean

    # Set up working directory
    ARG FUNCTION_DIR="/var/task"
    WORKDIR ${FUNCTION_DIR}

    # Copy requirements for Python and R scripts
    COPY requirements.txt .
    COPY script_calcproper.R .
    COPY main.py .
    COPY data/. data/.

    # Install Python dependencies
    RUN pip install --no-cache-dir -r requirements.txt

    # Stage 2: Final stage
    FROM python:3.10-slim-bullseye

    ENV DEBIAN_FRONTEND=noninteractive

    # Set up environment variables
    ENV LC_ALL=C.UTF-8
    ENV LANG=en_US.UTF-8
    ENV TZ=:/etc/localtime
    ENV PATH=/var/lang/bin:/usr/local/bin:/usr/bin/:/bin:/opt/bin
    ENV LD_LIBRARY_PATH=/var/lang/lib:/lib64:/usr/lib64:/var/runtime:/var/runtime/lib:/var/task:/var/task/lib:/opt/lib

    # Install R runtime dependencies in final stage
    RUN apt-get update && \
    apt-get install -y --no-install-recommends \
    r-base-core \
    libcurl4-openssl-dev \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

    # Define custom function directory
    ARG FUNCTION_DIR="/var/task"
    WORKDIR ${FUNCTION_DIR}

    # Copy only the necessary files from the build stage
    COPY --from=builder /usr/local/lib/python3.10/site-packages /usr/local/lib/python3.10/site-packages
    COPY --from=builder /usr/local/bin /usr/local/bin
    COPY --from=builder ${FUNCTION_DIR} ${FUNCTION_DIR}

    # Set the CMD to your handler
    ENTRYPOINT [ "/usr/local/bin/python", "-m", "awslambdaric" ]
    CMD [ "main.handler"]


    any suggestions as to why the cold start of my fcn is still so slow? My requirements.txt packages are pandas, rpy2, boto3 and awslambdric.

    Continue reading...

Compartilhe esta Página