Running Streamlit on Serverless

I’m thinking of running Streamlit on serverless infrastructure, e.g. AWS Lambda. Has anyone tried this, and is this possible?

1 Like

Hi @keanekwa, thanks for your questions. We haven’t tried so far, but it would not work as it is. I am personally not an expert, but Streamlit is based on a Webasocket server which I am not sure how it would fit in a serverless architecture. Many people deployed on Heroku, GKE and various flavors of Azure, but not on Serverless that I am aware of.

Matteo

1 Like

For tutorials on deployment take a look at the resources tagged deployment at awesome-streamlit.org

1 Like

Would it be possible to use https://erm.github.io/mangum/ (adapter for using ASGI applications with AWS Lambda & API Gateway) to run Streamlit? Some people were able to run FastAPI this way: https://iwpnd.pw/articles/2020-01/deploy-fastapi-to-aws-lambda.

Thanks!

Hi @nicolasmetallo,

Thanks for the question.

Streamlit is currently built using Tornado, which does not natively support the ASGI interface. If Tornado were to include an ASGI adapter, then we’d likely do so as well.

If you’re finding ASGI support to be an important aspect of whether you want to use Streamlit or not, I would recommend filing an enhancement request on our github repo. We are working on making Streamlit a lot easier to deploy over the course of this year, especially with our Streamlit4Teams rollout.

Thanks again!

API Gateway supports websockets. Conceptually, package streamlit’s python script in a lmabda handler, then install streamlit dependencies in a lmbda layer - finally connect the lambda to an API Gateway with websocket enabled - do you still see issue with this approach ?

One of the major issues for data scientist is the hassle for provisioning infrastructure and deploying their code. If this can be deplyed in a serverless environment , that is one less concern to worry for them. Hence my preference to prove out an API-GW-Lambda approach.

An article was just published about using Streamlit with AWS Fargate:

1 Like

hey,

thats a great article, and i’m not knocking it, but using ECS means you’re still running a service 24x7. So i would argue that’s not really serverless.

I would envisage a way to expose some sort of data api, and then a lambda (?) where necessary to perform the functions that the streamlit server currently does.

Maybe the current architecture is not yet ready for this? be good to consider for the future though. And in reality the above really sounds to me like you need a client side tool, and a data api and then mash them together. i.e. push all the streamlit logic to the client.

Now that AWS lambda supports deploying containers, I have been trying to deploy a streamlit docker container using Lambda following this tutorial. The only problem that I am facing is that the handler for Lambda expects something to be returned, but I want to trigger Streamlit there. Currently, I’ve been trying to do it through subprocess:

def handler():
    subprocess.run('streamlit run serve.py')

However, this obviously doesn’t return anything and causes a timeout error.
The Dockerfile looks something like this:

ARG FUNCTION_DIR="/function"

# base image
FROM python:3.7.9-buster as build-image
# FROM amazon/aws-lambda-python:3.7

# Install aws-lambda-cpp build dependencies
RUN apt-get update && \
  apt-get install -y \
  g++ \
  make \
  cmake \
  unzip \
  libcurl4-openssl-dev

EXPOSE 8501

.
.
.

# Set working directory to function root directory
WORKDIR ${FUNCTION_DIR}

# copy the requirements file to the working directory
COPY requirements.txt .

# install dependencies
RUN pip install -r requirements.txt

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

Any help would be highly appreciated. Thanks!

3 Likes