
In this tutorial we will see how to deploy FastAPI on Azure App Service Linux Plan. The FastAPI will have PostgreSQL database and Asynchronous REST Endpoints. We will create a PostgreSQL database on Azure and App Service Plan to host FastAPI on Azure βοΈ.
In case you are interested in development of the FastAPI app that is being deployed in this tutorial, it is recommended to go through the article that will walk you through Implementing Async REST APIs in FastAPI with PostgreSQL CRUD.
- Prerequisites
- Architectural Overview of Deploying FastAPI on Azure App Service
- Create & Configure Azure Database for PostgreSQL server
- Create & Configure Azure App Service (Linux)
- Configure to Deploy FastAPI on Azure App Service from GitHub
- Video Tutorial
Prerequisites
- Internet Connectivity
- GithubΒ account
- Fork thisΒ repositoryΒ to your GitHub account
- Azure Account
- Azure App Service (Linux, F1: Free or B1: Basic)
- Azure Database for PostgreSQL servers (Basic, 1 vCore(s), 50 GB)
We will accomplish the deployment of FastAPI on Azure βοΈ that has asynchronous REST Endpoints and PostgreSQL database as a persistence in three steps
- Create & Configure Azure Database for PostgreSQL server
- Create & Configure Azure App Service (Linux)
- Configure to Deploy FastAPI on Azure App Service from GitHub
Architectural Overview of Deploying FastAPI on Azure App Service
The following image shows the Architecture of how the FastAPI is deployed on Azure App Service.

Once we have deployed, our FastAPI running on Azure App Service can be visualized as mentioned in the architectural diagram.
The web request can be made to Azure App Service which can be accessed via an URL of the form {your-app-service-name}.azurewebsites.net unless you configure Custom Domain to the App Service.
The request is received by Gunicorn which spwans the FastAPI running on four child worker processes with the help of Asynchronous Uvicorn Worker Class. Each of the Uvicorn worker class runs FastAPI app on a randomly chosen process id (pid) and the request delegation is handled by the Gunicorn that runs on a process id that can be configured to run on specified port.
The web request is delegated to one of the instance of the FastAPI application that is available among these worker processes. All the four instances in this setup will talk to the same database created in the Azure Database for PostgreSQL Server. The database context for each of the worker processes is isolated from the other, because we are establishing the connection to the database in the @app.on_event("startup")
event of the FastAPI. Similarly we are disconnecting from the database in @app.on_event("shutdown")
event of the FastAPI. The startup and shutdown events are invoked for each of the worker processes. Thus any requests that involve interaction with the database are handled within the process.
The App service deployment configuration will detect any changes to the GitHub repository it is configured with. The deployment configuration with Kudu pulls and deploys the code to the App Service automatically.
Full Source Code of this tutorial is available onΒ fastapi-postgresql-azure-deploy
branch of thisΒ repository.
Create & Configure Azure Database for PostgreSQL server
Create PostgreSQL server on Azure
Follow these steps to create Azure Database for PostgreSQL server.
- Login toΒ portal.azure.com
- Type PostgreSQL in the search box located at the top center of Azure Portal and chooseΒ
Azure Database for PostgreSQL servers
Β in theΒServices
Β section of search results. - Click onΒ
Add
Β button to a new PostgreSQL server - Select Azure Database for PostgreSQL deployment option depending on your need. Iβll be choosingΒ
Single Server
Β and clickΒCreate
. - Under theΒ
Basics
Β tab ofΒSingle Server
Β for PostgreSQL configuration wizard provide Project Details, Server Details
Provide Project Details
- Choose Subscription of your choice in case you have multiple options or proceed with default chosen option.
- For Resource Group, click on Create new and give a meaningful name such asΒ
FastAPI-PostgreSQL-rg
A Resource group provides logical grouping of all the resources you want to maintain for your project. Any new services or features that you want to add that belongs to this project (such as PostgreSQL database or Storage Accounts) can be created and associated to this Resource Group.
Provide Server Details
Enter required settings for this server, including picking a location and configuring the compute and storage resources.
- ProvideΒ
Server name
Β asΒfastapi-pgsql-srv
Β or any other name of your choice - Leave theΒ
Data source
Β option as is set toΒNone
- ChooseΒ
Location
Β of your choice or go on with the default chosen option - Choose PostgreSQL serverΒ
Version
Β of 10 or above - ForΒ
Compute + storage
Β click onΒConfigure server
Β that will land you or server Configure blade - Click onΒ
Basic
Β tab among three available tabsΒBasic
,ΒGeneral Purpose
Β andΒMemory Optimized
- SlideΒ
vCore
Β to have cores based on your need. I moved the slider to choose 1 vCore - SlideΒ
Storage
Β to increase or decrease theΒBasic storage
Β capacity for your PostgreSQL database server - Leave all other options as is and click onΒ
OK
Create Administrator account for Azure database for PostgreSQL
Provide credentials that will allow you to connect to the PostgreSQL database server.
- ProvideΒ
Admin username
Β of your choice - ProvideΒ
Password
Β and ensure theΒConfirm password
Β to match password provided - Click onΒ
Review + create
Β and then clickΒCreate
- The database server creation will typically take 5-7 minutes and hold back till the resource creation completes
- Click onΒ
Go to Resource
Β or manually navigate to the PostgreSQL server created
Configure Azure database for PostgreSQL server
Navigate to the PostgreSQL server created and you will be landed on Overview page. UnderΒ Settings
Β click onΒ Connection security
Allow access to Azure services
If you want this database server to be access by all Azure Services across Azure Portal including the resources that are not in active subscription where this server lies, toggle theΒ Allow access to Azure services
Β toΒ Yes
. Iβll leave it asΒ No
Β which is default as my web app will be within the same subscription that of the database server.
Access Azure PostgreSQL database server from pgAdmin on local PC
In order for you to access the database server on your PC via pgAdmin, then click onΒ Add client IP
Β located at the top of theΒ Connection security
Β page. This will add a Firewall rule name with the IP address of your PC / Laptop from where you want to access this database server via pgAdmin.
- Leave the Enforce SSL settings toΒ
ENABLED
Β unless you want to turn it off for obvious reasons - Finally, click onΒ
Save
Β button located at the top of theΒConnection security
page
Connect to Azure PostgreSQL Database server from pgAdmin
If you do not have PostgreSQL on your PC you may want to install it by following the instructions mentioned in this articleΒ Install PostgreSQL Without Admin Rights on Windows 10 OS
Open Create-Server Wizard
Under the Servers tree node to the extreme left, right click on the node, choose create and click on Server⦠to create a new server. You can also click Add New Server under Quick Links that appears in the center of the page.
Specify a Server Name
Under General tab of the Create β Server wizard, specify the name of the server you want to create. This could be any noticeable name of your choice or something that might indicate the logical name of the data that the database server holds.
Specify Connection Details
To provide values for Connection details, open the Overview page of the PostgreSQL database server on Azure.
- ProvideΒ
Host name/address
Β with value ofΒServer name
Β field. - ProvideΒ
Port
Β with value ofΒ5432
- ProvideΒ
Username
Β with value ofΒAdmin username
- ProvideΒ
Password
Β with value given when creating Administrator account with username and password. - If you forgot the password, in Overview pane of Azure PostgreSQL server find and clickΒ
Reset password
Β and then provideΒPassword
Β andΒConfirm password
Β and save it and provide the same in password textbox of pgAdmin Connection detail.
SSL Configuration
Azure database for PostgreSQL server has default configuration toΒ Enforce SSL connection
. So in order for you to be able to connect to it from pgAdmin perform the following while creating server on pgAdmin to connect to it.
- Click on the SSL tab of theΒ
Create - Server
Β popup of pgAdmin - Change the value ofΒ
SSL mode
Β dropdown fromΒPrefer
Β toΒRequire
- Finally Click on Save and you should be able to see the Azure PostgreSQL server visible in the pgAdmin Servers list
- The new server will appear in the left pane under the Servers tree as a child node
Create a database in PostgreSQL via pgAdmin4
- Under the databases you will find the defaultΒ
postgres
Β database - Locate and right click on the database node of the Azure database for PostgreSQL server in pgAdmin. Choose create and then click on
Databaseβ¦
- In the General tab of the Create β Database wizard, give a name of your choice to be set for the new database being created. Feel free to modify the encoding in the Definition tab of the wizard. By default it will be set to UTF-8. I will be giving the database name asΒ
fastapi
- Click on Save to create the database and you should see the new database under the server we have created
Create & Configure Azure App Service (Linux)
Create Azure App Service
Follow the steps to create Azure app service for Linux.
- Login toΒ portal.azure.com
- Locate App Services and Click on Add
Provide Project Details
- On Create Web App wizard under Basics tab choose Subscription in case you have multiple options
- For Resource Group, click on Create new and give a meaningful name such asΒ
FastAPI-PostgreSQL-rg
A Resource group provides logical grouping of all the resources you want to maintain for your project. Any new services or features that you want to add that belongs to this project (such as PostgreSQL database or Storage Accounts) can be created and associated to this Resource Group.
Provide Instance Details
- Provide meaningful name for your FastAPI deployment on Azure App service. It should be unique across all the web app names available in the Azure. Iβll go on withΒ
fastapi-pgsql-demo
Β and once the FastAPI is deployed on Azure app service, I will be able to access it via the urlΒfastapi-pgsql-demo.azurewebsites.net
. You can provide the App Service name of your choice - ForΒ
Publish mode
Β chooseΒCode
. Surprise, no docker is being used here! - ChooseΒ
Runtime stack
Β to be Python 3.6 or higher. I will be choosing Python 3.8 - Choose Linux asΒ
Operating System
- Leave theΒ
Region
Β as default chosen option unless you want to select a different region of your choice
Choose App Service Plan
- ForΒ
Linux Plan (Region)
Β click on Create new and Provide a meaningful name for your App Service Plan. Iβll be naming itΒfastapi-pgsql-asp
- ForΒ
Sku and size
Β click onΒChange size
Β link and you will land onΒSpec Picker
Β blade. For production workloads you can choose among any of the Standard (S1 / S2 / S3) or Premium (P1V2 / P2V2 / P3V2) pricing tiers. But for the sake of tutorial and save some money π I will go on with the B1. You can also choose the Basic (B1) core for Linux which is free for the first 30 days from the date of creation of App Service Plan. Also, B1 and above plans facilitate to point custom domains or configure custom SSL to all the web apps hosted under this App Service Plan. Free F1 plan does not have these features. - UnderΒ
Dev / Test (For less demanding workloads)
Β tab ofΒSpec Picker
choose B1 located under Recommended pricing tiers and click onΒApply
. If you are following this tutorial for practice purposes, do not forget to delete all the resources under the resource group after the tutorial is over. - Click onΒ
Review + Create
Β and verify all the details and click onΒCreate
. The Web App creation will take 3-5 minutes.Once done click onΒGo to Resource
.
Configure Azure App Service
Open the App Service that you have created by manually navigating to it.
- UnderΒ
Settings
Β ->ΒConfiguration
Β open tabΒGeneral Settings
Β of App Service - Give theΒ
Startup Command
Β with the command to start FastAPI on Azure App Service
gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:app
- Navigate toΒ
Settings
Β ->ΒConfiguration
Β open tabΒApplication Settings
of App Service - Add new application settings with namesΒ
db_username
,Βdb_password
,Βhost_server
,Βdatabase_name
,Βssl_mode
Β andΒdb_server_port
Β and corresponding values for each of the settings with that of Azure database for PostgreSQL server created.
Configure to Deploy FastAPI on Azure App Service from GitHub
Perform the following steps to configure a Continuous Deployment of our FastAPI app hosted on GitHub on to Azure App Service. This will ensure that whenever a new commit is pushed to the configured branch, the new changes will be deployed to the Azure App Service.
Disclaimer: It is recommended to have tests and other sanity to be added as a part of the Continuous Delivery pipeline. Ensure to have all tests pass before deploying to avoid any unforeseen surprises.
- Navigate toΒ
Deployment
Β section of our Azure App Service for Linux that we have created and click onΒDeployment Center
- OnΒ
Continuous Deployment (CI / CD)
Β step, chooseΒGitHub
Β and if prompted, authorize your GitHub account by providing credentials and click onΒContinue
Β to proceed. - ChooseΒ
Build Provider
Β asΒApp Service build service
Β and click onΒContinue
. - InΒ
Configure
Β step, we will tell where to pullΒCode
Β from with details as below.Organization
Β β If you have multiple organizations, choose the organization where your FastAPI repo is located. Otherwise go on with default Organization chosen.Repository
Β β Locate and choose the repository based on the name.Branch
Β β Choose the branch to deploy to the Azure App Service every time a change is pushed to.- Click onΒ
Continue
Β to proceed.
- InΒ
Summary
Β step, verify Source Control properties and build provider and clickΒFinish
. - Wait for the first initial build to pull the code from configured GitHub repository until you see statusΒ
Success (Active)
.Azure App Service Deployment runs theΒ oryx build, an intelligent rule based application builder that has some predefined configurations to detect the code base to be built and performs suitable actions. For example if you are trying to deploy a Python based application, it will automagically install virtual environment and looks for the presence ofrequirements.txt
if any and installs the dependencies. It also try to spin up the application by looking forΒmain.py
orΒapp.py
Β using python interpreter.
After the successful deployment of FastAPI on Azure App service, navigate toΒ your-app-service-name.azurewebsites.net/docs
Β to access the OpenAPI (formerly known as Swagger) spec.
Video Tutorial
How to Deploy FastAPI on Azure App Service in just 30 minutes
Congratulations π, you have successfully learnt how to deploy FastAPI on Azure App Service. Bookmark π (Ctrl + D) this page for a quick reference.
Check these resources to deploy FastAPI on On Premises and other Cloud Platforms
[β¦] App Services running. Two of the Azure App Services were hosting WordPress and rest other are dummy Python based REST APIs using FastAPI , Flask that no one use. Out of the two WordPress Web App Services, one has mediocre ~150 to ~200 [β¦]
[β¦] Deploy FastAPI with Async REST Endpoints with PostgreSQL database as backend to Azure App Service (Lβ¦ [β¦]
[β¦] Deploy FastAPI on Azure App Service β TutLinks [β¦]
[β¦] Deploy FastAPI on Azure App Service β TutLinks [β¦]
[β¦] Deploy FastAPI on Azure App Service β TutLinks [β¦]
[β¦] If you are new to Azure App Service, and wish to learn how to deploy your FastAPI code to Azure App Services, you can find a good tutorial here. [β¦]
[β¦] you have deployed a FastAPI based App Service on Azure[β¦]