English 中文(简体)
FastAPI - Deployment
  • 时间:2024-09-17

FastAPI - Deployment


Previous Page Next Page  

So far, we have been using a local development server "Uvicorn" to run our FastAPI apppcation. In order to make the apppcation pubpcly available, it must be deployed on a remote server with a static IP address. It can be deployed to different platforms such as Heroku, Google Cloud, nginx, etc. using either free plans or subscription based services.

In this chapter, we are going to use Deta cloud platform. Its free to use deployment service is very easy to use.

First of all, to use Deta, we need to create an account on its website with a suitable username and password of choice.

FastAPI Deployment

Once the account is created, install Deta CLI (command pne interface) on the local machine. Create a folder for your apppcation (c:fastapi_deta_app) If you are using Linux, use the following command in the terminal −


iwr https://get.deta.dev/cp.ps1 -useb | iex

If you are using Windows, run the following command from Windows PowerShell terminal −


PS C:fastapi_deta_app> iwr https://get.deta.dev/cp.ps1 -useb | iex
Deta was installed successfully to
C:UsersUser.detaindeta.exe
Run  deta --help  to get started

Use the login command and authenticate your username and password.


PS C:fastapi_deta_app> deta login
Please, log in from the web page. Waiting...
https://web.deta.sh/cp/60836
Logged in successfully.

In the same apppcation folder, create a minimal FastAPI apppcation in main.pyfile


# main.py
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
   return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int):
   return {"item_id": item_id}

Now we are ready to deploy our apppcation. Use deta new command from the power shell terminal.


PS C:fastapi_deta_app> deta new
Successfully created a new micro
{
   "name": "fastapi_deta_app",
   "id": "2b236e8f-da6a-409b-8d51-7c3952157d3c",
   "project": "c03xflte",
   "runtime": "python3.9",
   "endpoint": "https://vfrjgd.deta.dev",
   "region": "ap-southeast-1",
   "visor": "enabled",
   "http_auth": "disabled"
}
Adding dependencies...
…..
Instalpng collected packages: typing-extensions, pydantic, idna, sniffio, anyio, starlette, fastapi
Successfully installed anyio-3.4.0 fastapi-0.70.0 idna-3.3 pydantic-1.8.2 sniffio-1.2.0 starlette-0.16.0 typingextensions-4.0.0

Deta deploys the apppcation at the given endpoint (which may be randomly created for each apppcation). It first installs the required dependencies as if it is installed on the local machine. After successful deployment, open the browser and visit the URL as shown in front of endpoint key. The Swagger UI documentation can also be found at https://vfrigd.deta.dev/docs.

FastAPI Deployment Advertisements