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" }
  ]
FastAPI with async for generating QR codes and bolt11 for Lightning Addresses

sendsats An API for getting QR codes and Bolt11 Invoices from Lightning Addresses. Share anywhere; as a link for tips on a twitter profile, or via mes

Bitkarrot 12 Jan 07, 2023
🍃 A comprehensive monitoring and alerting solution for the status of your Chia farmer and harvesters.

chia-monitor A monitoring tool to collect all important metrics from your Chia farming node and connected harvesters. It can send you push notificatio

Philipp Normann 153 Oct 21, 2022
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
Turns your Python functions into microservices with web API, interactive GUI, and more.

Instantly turn your Python functions into production-ready microservices. Deploy and access your services via HTTP API or interactive UI. Seamlessly export your services into portable, shareable, and

Machine Learning Tooling 2.8k Jan 04, 2023
Reusable utilities for FastAPI

Reusable utilities for FastAPI Documentation: https://fastapi-utils.davidmontague.xyz Source Code: https://github.com/dmontagu/fastapi-utils FastAPI i

David Montague 1.3k Jan 04, 2023
A RESTful API for creating and monitoring resource components of a hypothetical build system. Built with FastAPI and pydantic. Complete with testing and CI.

diskspace-monitor-CRUD Background The build system is part of a large environment with a multitude of different components. Many of the components hav

Nick Hopewell 67 Dec 14, 2022
Instrument your FastAPI app

Prometheus FastAPI Instrumentator A configurable and modular Prometheus Instrumentator for your FastAPI. Install prometheus-fastapi-instrumentator fro

Tim Schwenke 441 Jan 05, 2023
API written using Fast API to manage events and implement a leaderboard / badge system.

Open Food Facts Events API written using Fast API to manage events and implement a leaderboard / badge system. Installation To run the API locally, ru

Open Food Facts 5 Jan 07, 2023
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
Middleware for Starlette that allows you to store and access the context data of a request. Can be used with logging so logs automatically use request headers such as x-request-id or x-correlation-id.

starlette context Middleware for Starlette that allows you to store and access the context data of a request. Can be used with logging so logs automat

Tomasz Wójcik 300 Dec 26, 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
SuperSaaSFastAPI - Python SaaS Boilerplate for building Software-as-Service (SAAS) apps with FastAPI, Vue.js & Tailwind

Python SaaS Boilerplate for building Software-as-Service (SAAS) apps with FastAP

Rudy Bekker 31 Jan 10, 2023
A server hosts a FastAPI application and multiple clients can be connected to it via SocketIO.

FastAPI_and_SocketIO A server hosts a FastAPI application and multiple clients can be connected to it via SocketIO. Executing server.py sets up the se

Ankit Rana 2 Mar 04, 2022
A minimal FastAPI implementation for Django !

Caution!!! This project is in early developing stage. So use it at you own risk. Bug reports / Fix PRs are welcomed. Installation pip install django-m

toki 23 Dec 24, 2022
Flask-vs-FastAPI - Understanding Flask vs FastAPI Web Framework. A comparison of two different RestAPI frameworks.

Flask-vs-FastAPI Understanding Flask vs FastAPI Web Framework. A comparison of two different RestAPI frameworks. IntroductionIn Flask is a popular mic

Mithlesh Navlakhe 1 Jan 01, 2022
The template for building scalable web APIs based on FastAPI, Tortoise ORM and other.

FastAPI and Tortoise ORM. Powerful but simple template for web APIs w/ FastAPI (as web framework) and Tortoise-ORM (for working via database without h

prostomarkeloff 95 Jan 08, 2023
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
High-performance Async REST API, in Python. FastAPI + GINO + Arq + Uvicorn (w/ Redis and PostgreSQL).

fastapi-gino-arq-uvicorn High-performance Async REST API, in Python. FastAPI + GINO + Arq + Uvicorn (powered by Redis & PostgreSQL). Contents Get Star

Leo Sussan 351 Jan 04, 2023
A FastAPI Plug-In to support authentication authorization using the Microsoft Authentication Library (MSAL)

FastAPI/MSAL - MSAL (Microsoft Authentication Library) plugin for FastAPI FastAPI - https://github.com/tiangolo/fastapi FastAPI is a modern, fast (hig

Dudi Levy 15 Jul 20, 2022
Hyperlinks for pydantic models

Hyperlinks for pydantic models In a typical web application relationships between resources are modeled by primary and foreign keys in a database (int

Jaakko Moisio 10 Apr 18, 2022