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" }
  ]
Restful Api developed with Flask using Prometheus and Grafana for monitoring and containerization with Docker :rocket:

Hephaestus πŸš€ In Greek mythology, Hephaestus was either the son of Zeus and Hera or he was Hera's parthenogenous child. ... As a smithing god, Hephaes

Yasser Tahiri 16 Oct 07, 2022
LuSyringe is a documentation injection tool for your classes when using Fast API

LuSyringe LuSyringe is a documentation injection tool for your classes when using Fast API Benefits The main benefit is being able to separate your bu

Enzo Ferrari 2 Sep 06, 2021
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
Deploy/View images to database sqlite with fastapi

Deploy/View images to database sqlite with fastapi cd realistic Dependencies dat

Fredh Macau 1 Jan 04, 2022
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
Generate Class & Decorators for your FastAPI project βœ¨πŸš€

Classes and Decorators to use FastAPI with class based routing. In particular this allows you to construct an instance of a class and have methods of that instance be route handlers for FastAPI & Pyt

Yasser Tahiri 34 Oct 27, 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
Beyonic API Python official client library simplified examples using Flask, Django and Fast API.

Beyonic API Python Examples. The beyonic APIs Doc Reference: https://apidocs.beyonic.com/ To start using the Beyonic API Python API, you need to start

Harun Mbaabu Mwenda 46 Sep 01, 2022
User authentication fastapi with python

user-authentication-fastapi Authentication API Development Setup environment You should create a virtual environment and activate it: virtualenv venv

Sabir Hussain 3 Mar 03, 2022
Flask + marshmallow for beautiful APIs

Flask-Marshmallow Flask + marshmallow for beautiful APIs Flask-Marshmallow is a thin integration layer for Flask (a Python web framework) and marshmal

marshmallow-code 768 Dec 22, 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
FastAPI interesting concepts.

fastapi_related_stuffs FastAPI interesting concepts. FastAPI version :- 0.70 Python3 version :- 3.9.x Steps Test Django Like settings export FASTAPI_S

Mohd Mujtaba 3 Feb 06, 2022
Cube-CRUD is a simple example of a REST API CRUD in a context of rubik's cube review service.

Cube-CRUD is a simple example of a REST API CRUD in a context of rubik's cube review service. It uses Sqlalchemy ORM to manage the connection and database operations.

Sebastian Andrade 1 Dec 11, 2021
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
Docker Sample Project - FastAPI + NGINX

Docker Sample Project - FastAPI + NGINX Run FastAPI and Nginx using Docker container Installation Make sure Docker is installed on your local machine

1 Feb 11, 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
μŠ€νƒ€νŠΈμ—… 개발자 μ±„μš©

μŠ€νƒ€νŠΈμ—… 개발자 μ±„μš© 倧 λ°•λžŒνšŒ Seed ~ Series B에 μžˆλŠ” μŠ€νƒ€νŠΈμ—…μ„ μœ„ν•œ μ±„μš©μ •λ³΄ νŽ˜μ΄μ§€μž…λ‹ˆλ‹€. Back-end, Frontend, Mobile λ“± 개발자λ₯Ό λŒ€μƒμœΌλ‘œ μ§„ν–‰ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€. ν•΄λ‹Ή μŠ€νƒ€νŠΈμ—…μ— μ’…μ‚¬ν•˜μ‹œλŠ” λΆ„λΏλ§Œ μ•„λ‹ˆλΌ μ±„μš© κ΄€λ ¨ 정보λ₯Ό μ•Œκ³  κ³„μ‹œλ‹€λ©΄

JuHyun Lee 58 Dec 14, 2022
Opinionated authorization package for FastAPI

FastAPI Authorization Installation pip install fastapi-authorization Usage Currently, there are two models available: RBAC: Role-based Access Control

Marcelo Trylesinski 18 Jul 04, 2022
Lightning FastAPI

Lightning FastAPI Lightning FastAPI framework, provides boiler plates for FastAPI based on Django Framework Explaination / | β”‚ manage.py β”‚ README.

Rajesh Joshi 1 Oct 15, 2021
A Python framework to build Slack apps in a flash with the latest platform features.

Bolt for Python A Python framework to build Slack apps in a flash with the latest platform features. Read the getting started guide and look at our co

SlackAPI 684 Jan 09, 2023