All Projects → paul121 → fastapi-zeit-now

paul121 / fastapi-zeit-now

Licence: other
A simple example of deploying FastAPI as a Zeit Serverless Function

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to fastapi-zeit-now

serverless-mangum-examples
Example ASGI applications and Serverless Framework configurations using Mangum
Stars: ✭ 26 (+8.33%)
Mutual labels:  asgi, starlette, fastapi
msgpack-asgi
Drop-in MessagePack support for ASGI applications and frameworks
Stars: ✭ 100 (+316.67%)
Mutual labels:  asgi, starlette, fastapi
arel
Lightweight browser hot reload for Python ASGI web apps
Stars: ✭ 69 (+187.5%)
Mutual labels:  asgi, starlette, fastapi
starlette-graphene3
An ASGI app for using Graphene v3 with Starlette / FastAPI
Stars: ✭ 52 (+116.67%)
Mutual labels:  asgi, starlette, fastapi
inboard
🚢 Docker images and utilities to power your Python APIs and help you ship faster. With support for Uvicorn, Gunicorn, Starlette, and FastAPI.
Stars: ✭ 106 (+341.67%)
Mutual labels:  asgi, starlette, fastapi
webargs-starlette
Declarative request parsing and validation for Starlette with webargs
Stars: ✭ 36 (+50%)
Mutual labels:  asgi, starlette
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 automatically use request headers such as x-request-id or x-correlation-id.
Stars: ✭ 320 (+1233.33%)
Mutual labels:  starlette, fastapi
mongox
Familiar async Python MongoDB ODM
Stars: ✭ 113 (+370.83%)
Mutual labels:  asgi, starlette
async-asgi-testclient
A framework-agnostic library for testing ASGI web applications
Stars: ✭ 123 (+412.5%)
Mutual labels:  asgi, starlette
fastapi-project
FastAPI application without global variables(almost) =)
Stars: ✭ 26 (+8.33%)
Mutual labels:  asgi, fastapi
starlite
Light, Flexible and Extensible ASGI API framework
Stars: ✭ 1,525 (+6254.17%)
Mutual labels:  asgi, starlette
hypercorn-fastapi-docker
Docker image with Hypercorn for FastAPI apps in Python 3.7, 3.8, 3.9. Ready for HTTP2 and HTTPS
Stars: ✭ 18 (-25%)
Mutual labels:  asgi, fastapi
fastapi-websocket-broadcast
Websocket 'broadcast' demo using FastAPI/Starlette
Stars: ✭ 106 (+341.67%)
Mutual labels:  starlette, fastapi
fastAPI-aiohttp-example
How to use and test fastAPI with an aiohttp client
Stars: ✭ 69 (+187.5%)
Mutual labels:  asgi, fastapi
starlette-opentracing
Opentracing support for Starlette and FastApi
Stars: ✭ 62 (+158.33%)
Mutual labels:  starlette, fastapi
morelia server
Server for MoreliaTalk network
Stars: ✭ 25 (+4.17%)
Mutual labels:  starlette, fastapi
Fastapi
FastAPI framework, high performance, easy to learn, fast to code, ready for production
Stars: ✭ 39,588 (+164850%)
Mutual labels:  starlette, fastapi
Awesome Fastapi
A curated list of awesome things related to FastAPI
Stars: ✭ 3,033 (+12537.5%)
Mutual labels:  starlette, fastapi
TikTokDownloader PyWebIO
🚀「Douyin_TikTok_Download_API」是一个开箱即用的高性能异步抖音|TikTok数据爬取工具,支持API调用,在线批量解析及下载。
Stars: ✭ 919 (+3729.17%)
Mutual labels:  asgi, fastapi
fastapi-azure-auth
Easy and secure implementation of Azure AD for your FastAPI APIs 🔒 B2C, single- and multi-tenant support.
Stars: ✭ 174 (+625%)
Mutual labels:  asgi, fastapi

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" }
  ]
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].