awslabs / aws-lambda-web-adapter

Run web applications on AWS Lambda
Apache License 2.0
1.84k stars 108 forks source link

Slow init/scaling #358

Closed four43 closed 8 months ago

four43 commented 8 months ago

Hello,

First of all this is a great project! We absolutely love it and it extends the functionality of Lambda really nicely in a relatively simple way.

We are experiencing slow init times however when using this in our environment.

Lambda base image init: 250ms with some spikes up to 2000ms Lambda base image with lambda-web-adapter starting FastAPI: 5111ms! (This is very consistent for a cold start)

The FastAPI code being started is their very basic hello world getting started example:

test_api/__init__.py

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
async def root():
    return {"message": "Hello World"}

And it's started from the Dockerfile:

FROM aerisweather/geo-base-python3.10:base-v8
WORKDIR /app

RUN pip install fastapi "uvicorn[standard]"

## Enable Lambda triggered web server
COPY scripts/lambda-adapter /opt/extensions/lambda-adapter
ENV PORT=8000

COPY ./test_api ./test_api

ENTRYPOINT [ ]
CMD exec uvicorn --host 0.0.0.0 --port=$PORT --forwarded-allow-ips='*' --workers=1 test_api:app

The lambda logs don't seem to reflect the long startup init process: image

Any tips on speeding this up?

Thanks a lot!

bnusunny commented 8 months ago

Hmm, I usually see high cold start times in the first a few cold starts after the function is created or updated. This is because of the cold cache in Lambda Service. However, the cold starts after that are much faster. See the test results here.

Could you deploy the FastAPI example and check the cold start time?

four43 commented 8 months ago

That is an awesome analysis in that other thread! Thank you so much for that. I apologize that didn't show up in my search. I think the discussion over there and your tips are enough. Thank you very much!