A simple example of deploying FastAPI as a Zeit Serverless Function

Overview

FastAPI Zeit Now

Deploy a FastAPI app as a Zeit Serverless Function.

This repo deploys the FastAPI SQL Databases Tutorial to demonstrate how a FastAPI app can be deployed using the Zeit Asynchronous Server Gateway Interface (ASGI).

View the live demo at: https://fastapi-zeit-now.paul121.now.sh/?name=GithubUser

Notes about this deployment:

  • FastAPI is configured to return a Cache-Control header set to no-cache for all responses. Because static caching is automatic with Zeit, this ensures the Zeit CDN doesn't cache anything for the purposes of this example. More on caching here.
  • This repo contains a sample sqlite database that has a few predefined users and items to demonstrate returning data from a database.
    • Note Due to the nature of a serverless deploy, the sqlite file cannot be written to so any POST requests attempting to modify the DB will fail.
    • In a production deploy, the FastAPI app would connect to a database hosted elsewhere.

This is merely an example of integration with Zeit. I'm not currently deploying any FastAPI apps in this way, but would like to consider it a possibility. Any thoughts, concerns or ideas for benchmarking are welcome!!

Background

After learning about Zappa I was inspired to learn more about hosting FastAPI as a server-less function:

Zappa makes it super easy to build and deploy server-less, event-driven Python applications (including, but not limited to, WSGI web apps) on AWS Lambda + API Gateway. Think of it as "serverless" web hosting for your Python apps. That means infinite scaling, zero downtime, zero maintenance - and at a fraction of the cost of your current deployments!

The problem is that Zappa only works with WSGI Python apps such as Flask and Django, not ASGI.

Google Cloud Run and AWS Elastic Beanstalk are other alternatives, but don't support ASGI either.

I came across Mangum which is similar to Zappa, except it supports ASGI apps. While this would likely work with FastAPI (or most any ASGI Python app) it also seems to make some decisions about how you structure your app. And it still requires quite a bit of configuration with AWS to get everything working. (more in this issue)

Zeit Now makes this all a bit easier. Develop locally with now dev and deploy with now --prod.

Configuration

With Zeit Now, we just need to configure a few things in now.json, run now --prod and FastAPI is deployed.

See Zeit Docs on Configuration

Requirements

Define pip install requirements in a requirements.txt file.

Routing

Looking into this, the hardest thing to configure was Zeit Routes:

By default, routing is defined by the filesystem of your deployment. For example, if a user makes a request to /123.png, and your now.json file does not contain any routes with a valid src matching that path, it will fallback to the filesystem and serve /123.png if it exists.

This makes great sense for most serverless apps, but doesn't work so well with FastAPI when one function needs to respond to multiple routes (figured out in this issue).

I couldn't get rewrite to work, but did have success routing all requests to one FastAPI function:

  "routes": [
    { "src": "/(.*)", "dest": "app/main.py" }
  ]

Defining Functions

By default Zeit also looks for Python apps in an index.py file at the root or in the /api directory. This is can be configured by adding a build configuration to now.json:

  "builds": [
    { "src": "/app/main.py", "use": "@now/python" }
  ]
Sample-fastapi - A sample app using Fastapi that you can deploy on App Platform

Getting Started We provide a sample app using Fastapi that you can deploy on App

Erhan BÜTE 2 Jan 17, 2022
Simple example of FastAPI + Celery + Triton for benchmarking

You can see the previous work from: https://github.com/Curt-Park/producer-consumer-fastapi-celery https://github.com/Curt-Park/triton-inference-server

Jinwoo Park (Curt) 37 Dec 29, 2022
Asynchronous event dispatching/handling library for FastAPI and Starlette

fastapi-events An event dispatching/handling library for FastAPI, and Starlette. Features: straightforward API to emit events anywhere in your code ev

Melvin 238 Jan 07, 2023
cookiecutter template for web API with python

Python project template for Web API with cookiecutter What's this This provides the project template including minimum test/lint/typechecking package

Hitoshi Manabe 4 Jan 28, 2021
Feature rich robust FastAPI template.

Flexible and Lightweight general-purpose template for FastAPI. Usage ⚠️ Git, Python and Poetry must be installed and accessible ⚠️ Poetry version must

Pavel Kirilin 588 Jan 04, 2023
Browse JSON API in a HTML interface.

Falcon API Browse This project provides a middleware for Falcon Web Framework that will render the response in an HTML form for documentation purpose.

Abhilash Raj 4 Mar 16, 2022
Opentracing support for Starlette and FastApi

Starlette-OpenTracing OpenTracing support for Starlette and FastApi. Inspired by: Flask-OpenTracing OpenTracing implementations exist for major distri

Rene Dohmen 63 Dec 30, 2022
A minimal Streamlit app showing how to launch and stop a FastAPI process on demand

Simple Streamlit + FastAPI Integration A minimal Streamlit app showing how to launch and stop a FastAPI process on demand. The FastAPI /run route simu

Arvindra 18 Jan 02, 2023
OpenAPI for Todolist RESTful API

swagger-client OpenAPI for Todolist RESTful API This Python package is automatically generated by the Swagger Codegen project: API version: 1 Package

Iko Afianando 1 Dec 19, 2021
A fast and durable Pub/Sub channel over Websockets. FastAPI + WebSockets + PubSub == ⚡ 💪 ❤️

⚡ 🗞️ FastAPI Websocket Pub/Sub A fast and durable Pub/Sub channel over Websockets. The easiest way to create a live publish / subscribe multi-cast ov

8 Dec 06, 2022
🚀 Cookiecutter Template for FastAPI + React Projects. Using PostgreSQL, SQLAlchemy, and Docker

FastAPI + React · A cookiecutter template for bootstrapping a FastAPI and React project using a modern stack. Features FastAPI (Python 3.8) JWT authen

Gabriel Abud 1.4k Jan 02, 2023
Code for my FastAPI tutorial

FastAPI tutorial Code for my video tutorial FastAPI tutorial What is FastAPI? FastAPI is a high-performant REST API framework for Python. It's built o

José Haro Peralta 9 Nov 15, 2022
Flask-Bcrypt is a Flask extension that provides bcrypt hashing utilities for your application.

Flask-Bcrypt Flask-Bcrypt is a Flask extension that provides bcrypt hashing utilities for your application. Due to the recent increased prevelance of

Max Countryman 310 Dec 14, 2022
fastapi-mqtt is extension for MQTT protocol

fastapi-mqtt MQTT is a lightweight publish/subscribe messaging protocol designed for M2M (machine to machine) telemetry in low bandwidth environments.

Sabuhi 144 Dec 28, 2022
A utility that allows you to use DI in fastapi without Depends()

fastapi-better-di What is this ? fastapi-better-di is a utility that allows you to use DI in fastapi without Depends() Installation pip install fastap

Maxim 9 May 24, 2022
Sample FastAPI project that uses async SQLAlchemy, SQLModel, Postgres, Alembic, and Docker.

FastAPI + SQLModel + Alembic Sample FastAPI project that uses async SQLAlchemy, SQLModel, Postgres, Alembic, and Docker. Want to learn how to build th

228 Jan 02, 2023
Dead-simple mailer micro-service for static websites

Mailer Dead-simple mailer micro-service for static websites A free and open-source software alternative to contact form services such as FormSpree, to

Romain Clement 42 Dec 21, 2022
This code generator creates FastAPI app from an openapi file.

fastapi-code-generator This code generator creates FastAPI app from an openapi file. This project is an experimental phase. fastapi-code-generator use

Koudai Aono 632 Jan 05, 2023
A simple example of deploying FastAPI as a Zeit Serverless Function

FastAPI Zeit Now Deploy a FastAPI app as a Zeit Serverless Function. This repo deploys the FastAPI SQL Databases Tutorial to demonstrate how a FastAPI

Paul Weidner 26 Dec 21, 2022
Adds integration of the Chameleon template language to FastAPI.

fastapi-chameleon Adds integration of the Chameleon template language to FastAPI. If you are interested in Jinja instead, see the sister project: gith

Michael Kennedy 124 Nov 26, 2022