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.
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:
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.
func init --worker-runtime python
func new --name HttpTrigger --template "HTTP trigger" --authlevel "anonymous"
function_app.py
to route requests to FastAPI app (as given in codebase).Deploying the FastAPI app to Azure Functions involves the following steps:
az login
Deploy the App:
zip -r function.zip .
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.
https://<YOUR_FUNCTION_APP_NAME>.azurewebsites.net/HttpTrigger
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:
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: