page_type: sample languages:
Azure Functions supports WSGI and ASGI-compatible frameworks with HTTP-triggered Python functions. This can be helpful if you are familiar with a particular framework, or if you have existing code you would like to reuse to create the Function app. The following is an example of creating an Azure Function app using FastAPI.
You can develop and deploy a function app using either Visual Studio Code or the Azure CLI. Make sure you have the required prerequisites for your preferred environment:
Clone or download this sample's repository, and open the fastapi-on-azure-functions
folder in Visual Studio Code or your preferred editor (if you're using the Azure CLI).
The code in the sample folder has already been updated to support use of the FastAPI. Let's walk through the changed files.
The requirements.txt
file has an additional dependency of the fastapi
module:
azure-functions
fastapi
The file host.json includes the a routePrefix
key with a value of empty string.
{
"version": "2.0",
"extensions": {
"http": {
"routePrefix": ""
}
}
}
The root folder contains function_app.py
which initializes an AsgiFunctionApp
using the imported FastAPI
app:
import azure.functions as func
from WrapperFunction import app as fastapi_app
app = func.AsgiFunctionApp(app=fastapi_app, http_auth_level=func.AuthLevel.ANONYMOUS)
In the WrapperFunction
folder, the __init__.py
file defines a FastAPI app in the typical way (no changes needed):
import azure.functions as func
import fastapi
app = fastapi.FastAPI()
@app.get("/sample")
async def index():
return {
"info": "Try /hello/Shivani for parameterized route.",
}
@app.get("/hello/{name}")
async def get_name(name: str):
return {
"name": name,
}
Create a Python virtual environment and activate it.
Run the command below to install the necessary requirements.
python -m pip install -r requirements.txt
If you are using VS Code for development, click the "Run and Debug" button or follow the instructions for running a function locally. Outside of VS Code, follow these instructions for using Core Tools commands directly to run the function locally.
Functions:
http_app_func: [GET,POST,DELETE,HEAD,PATCH,PUT,OPTIONS] http://localhost:7071//{*route}
Functions:
WrapperFunction: [GET,POST] http://localhost:7071/{*route}
Try out URLs corresponding to the handlers in the app, both the simple path and the parameterized path:
http://localhost:7071/sample
http://localhost:7071/hello/YourName
There are three main ways to deploy this to Azure:
azd
tool, run azd up
in the root of the project. You can also run azd pipeline config
to set up a CI/CD pipeline for deployment.All approaches will provision a Function App, Storage account (to store the code), and a Log Analytics workspace.
After deployment, test these different paths on the deployed URL:
http://<FunctionAppName>.azurewebsites.net/sample
http://<FunctionAppName>.azurewebsites.net/hello/Foo
You can call the URL endpoints using your browser (GET requests) or one one of these HTTP test tools:
[!CAUTION]
For scenarios where you have sensitive data, such as credentials, secrets, access tokens, API keys, and other similar information, make sure to use a tool that protects your data with the necessary security features, works offline or locally, doesn't sync your data to the cloud, and doesn't require that you sign in to an online account. This way, you reduce the risk around exposing sensitive data to the public.
Now you have a simple Azure Function App using the FastAPI framework, and you can continue building on it to develop more sophisticated applications.
To learn more about leveraging WSGI and ASGI-compatible frameworks, see Web frameworks.