This repository demonstrates how to deploy a FastAPI application to Azure Functions using the v2 model with a function_app.py
file. The goal of this project is to showcase the deployment process of a basic FastAPI app using Azure’s serverless computing capabilities.
- About
- Prerequisites
- FastAPI Application Setup
- Creating an Azure Function
- Deploying FastAPI to Azure Functions
- Testing the Deployment
- What I Learned
- Conclusion
- References
This is a learning project aimed at showcasing the deployment process of a FastAPI application to Azure Functions. I am working on FastAPI-related tasks at Mila AI, and this project helps me apply and demonstrate my knowledge of deploying cloud applications, focusing on the serverless approach using Azure.
Before starting, ensure you have the following:
- Azure CLI: Install Azure CLI.
- Azure Functions Core Tools: For running functions locally. Install here.
- FastAPI & Uvicorn: Install using:
pip install fastapi uvicorn
Once the FastAPI application code is set up with a basic structure, you'll need to ensure that it can handle HTTP requests when deployed as an Azure Function. The setup includes a simple FastAPI app with one or two routes to demonstrate the deployment.
We will use the v2 model of Azure Functions, which structures the project using a function_app.py
file, eliminating the need for a function.json
file.
Next, we’ll initialize the Azure Function and set up the HTTP trigger to route requests to our FastAPI app.
-
Initialize a new Azure Function in your project directory:
func init --worker-runtime python
-
Create an HTTP-triggered function:
func new --name HttpTrigger --template "HTTP trigger" --authlevel "anonymous"
-
Modify
function_app.py
to route requests to FastAPI app (as given in codebase).
Deploying the FastAPI app to Azure Functions involves the following steps:
-
Login to Azure via CLI:
az login
-
Deploy the App:
-
First, create a ZIP file of your function app:
zip -r function.zip .
-
Then, deploy the ZIP file to your Azure Function App:
az functionapp deployment source config-zip --name <YOUR_FUNCTION_APP_NAME> --resource-group <YOUR_RESOURCE_GROUP_NAME> --src function.zip
-
Once deployed, you can test your FastAPI app by accessing the provided function URL.
-
Get the URL: The Azure CLI will output the URL for function app after deployment. It will look something like:
https://<YOUR_FUNCTION_APP_NAME>.azurewebsites.net/HttpTrigger
-
Test the API: Use
curl
or Postman to send a request to your FastAPI endpoint:curl https://<YOUR_FUNCTION_APP_NAME>.azurewebsites.net/HttpTrigger
You should see a JSON response similar to:
{ "message": "Hello from FastAPI deployed on Azure Functions!" }
While working on this project, I gained hands-on experience with:
- Deploying FastAPI apps to Azure Functions using the serverless model.
- Understanding how WSGI middleware works to integrate FastAPI with Azure Functions.
- Using Azure CLI for managing resources, creating Function Apps, and deploying applications.
- Configuring FastAPI to work in a serverless environment with automatic scaling based on demand.
This project serves as a demonstration of deploying a simple FastAPI application to Azure Functions using the v2 model. While this project focuses on the deployment process, it can be extended into a full-featured API in the future.
Feel free to explore, contribute, or provide feedback on this repository!
This project was built based on the official Microsoft Azure documentation. You can learn more about deploying Python apps on Azure Functions from the following resources: