page_type | languages | products | description | title | author | urlFragment | |||
---|---|---|---|---|---|---|---|---|---|
sample |
|
|
This is a sample Azure Function app created with the FastAPI framework. |
Using FastAPI Framework with Azure Functions |
shreyabatra4, vrdmr |
azure-functions-python-create-fastapi-app |
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 Fast API.
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
and nest_asyncio
modules:
azure-functions
fastapi
nest_asyncio
The file host.json includes the a routePrefix
key with a value of empty string.
{
"version": "2.0",
"extensions": {
"http": {
"routePrefix": ""
}
}
}
Inside the WrapperFunction
folder, the file function.json
includes a route
key in the bindings:
{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
],
"route": "{*route}"
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
]
}
In that same folder, the __init__.py
file uses AsgiMiddleware
to redirect invocations to a FastAPI app with two routes defined.
import logging
import azure.functions as func
import nest_asyncio
from FastAPIApp import app # Main API application
nest_asyncio.apply()
@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,
}
async def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
"""Each request is redirected to the ASGI handler."""
return func.AsgiMiddleware(app).handle(req, context)
First run the command below to install the necessary requirements.
pip3 install -r requirements.txt
If you are using VS Code for development, follow the instructions for running a function locally. Otherwise, follow these instructions for using Core Tools commands directly to run the function locally.
Once the function is running, test the function at the local URL displayed in the Terminal panel:
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
If you are using VS Code for development, follow these instructions for using the extension to create resources and deploying to Azure. Otherwise, follow these instructions for using the Azure CLI to create resources and deploy to Azure.
Once deployed, test different paths on the deployed URL, using either a browser or a tool like Postman.
http://<FunctionAppName>.azurewebsites.net/sample
http://<FunctionAppName>.azurewebsites.net/hello/Foo
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.