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.
Here are some of the prerequisites to get this sample to work for you.
Install Python
A Python version that is supported by Azure Functions is required. Run python --version
(Linux/MacOS) or py --version
(Windows) to check your Python version reports to a supported version. For more information on installing Python, see How to install Python.
Install Azure Functions Core Tools
Azure Functions Core Tools provides commands to create functions, connect to Azure, and deploy function projects. For more information, see Install Azure Functions Core Tools.
Create a new Azure Function App in VS Code
To create an Azure Function app in VSCode, please go through the Microsoft Docs tutorial on creating your first Azure Function using Visual Studio Code. In the code snippet along with the sample, we name the two python module 'FastAPIApp' and 'WrapperFunction' with the HTTP trigger.
Clone or download this sample repository, and open the sample folder in Visual Studio Code or your IDE of choice.
The file requirements.txt is updated to include the following depdendencies.
azure-functions
fastapi
Note that azure-functions-worker
should not be included in this file as the Python worker is manager by Azure Functions platform and manually managing it may cause unexpected issues.
The following code shows the use of AsgiMiddleware
, which redirects the invocations to FastAPI handler.
import azure.functions as func
from FastAPIApp import app
import nest_asyncio
nest_asyncio.apply()
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)
The file function.json is modified to include route
in the HTTP trigger.
{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
],
"route": "/{*route}"
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
]
}
The file host.json is updated to include the HTTP routePrefix
.
{
"version": "2.0",
"extensions": {
"http": {
"routePrefix": ""
}
}
}
To run Function Apps using Core Tools, see Run functions locally with Core Tools.
To test locally, run the below to install FastAPI.
pip install -r requirements.txt
Then, start debug mode and test the function using the HTTP endpoint exposed after the host and the worker load up the function.
Http Functions:
HandleApproach: [GET,POST] https://localhost:7071/<route>
You can publish the function app directly from VSCode using the “Publish to Azure option” in the Azure extension. For more information, please refer the guide to publish the project to Azure using Visual Studio Code.
You can use a tool like Postman to see the API in action locally, and on Azure. Running locally will help you to verify the credentials, configuration and business logic.
When running this sample, you can try a different URL route as well as parameterize it. For instance, https://<HOST>:7071/hello/Foo
to call the FastAPI app with path param Foo
.
When done locally, please try the following URL in your browser -
https://localhost:7071/hello/Foo
When done in Azure, please try the following URL in your browser -
https://<FunctionAppName>.azurewebsites.net/hello/Foo
This sample helps you setup an app with the FastAPI framework and can help you get started using web frameworks in Azure Functions.
To learn more about altering Python functions to leverage WSGI and ASGI-compatible frameworks, see Web frameworks.