title | description | author | ms.topic | ms.date | ms.reviewer | ms.devlang | ms.custom | zone_pivot_groups |
---|---|---|---|---|---|---|---|---|
Create your first durable function in Azure using Python |
Create and publish an Azure Durable Function in Python using Visual Studio Code. |
davidmrdavid |
quickstart |
06/15/2022 |
azfuncdf, davidmrdavid |
python |
mode-api, devdivchpfy22, vscode-azure-extension-update-complete, devx-track-python |
python-mode-functions |
Durable Functions is an extension of Azure Functions that lets you write stateful functions in a serverless environment. The extension manages state, checkpoints, and restarts for you.
In this article, you learn how to use the Visual Studio Code Azure Functions extension to locally create and test a "hello world" durable function. This function will orchestrate and chains together calls to other functions. You can then publish the function code to Azure.
:::image type="content" source="./media/quickstart-python-vscode/functions-vs-code-complete.png" alt-text="Screenshot of the running durable function in Azure.":::
To complete this tutorial:
-
Install Visual Studio Code.
-
Install the Azure Functions Visual Studio Code extension.
-
Make sure that you have the latest version of the Azure Functions Core Tools.
-
Durable Functions require an Azure storage account. You need an Azure subscription.
-
Make sure that you have version 3.7, 3.8, 3.9, or 3.10 of Python installed.
[!INCLUDE quickstarts-free-trial-note]
In this section, you use Visual Studio Code to create a local Azure Functions project.
-
In Visual Studio Code, press F1 (or Ctrl/Cmd+Shift+P) to open the command palette. In the command palette, search for and select
Azure Functions: Create New Project...
.:::image type="content" source="media/quickstart-python-vscode/functions-create-project.png" alt-text="Screenshot of Create function window.":::
-
Choose an empty folder location for your project and choose Select.
::: zone pivot="python-mode-configuration"
-
Follow the prompts and provide the following information:
Prompt Value Description Select a language for your function app project Python Create a local Python Functions project. Select a version Azure Functions v4 You only see this option when the Core Tools aren't already installed. In this case, Core Tools are installed the first time you run the app. Python version Python 3.7, 3.8, 3.9, or 3.10 Visual Studio Code will create a virtual environment with the version you select. Select a template for your project's first function Skip for now Select how you would like to open your project Open in current window Reopens Visual Studio Code in the folder you selected.
::: zone-end
::: zone pivot="python-mode-decorators"
-
Follow the prompts and provide the following information:
Prompt Value Description Select a language Python (Programming Model V2) Create a local Python Functions project using the V2 programming model. Select a version Azure Functions v4 You only see this option when the Core Tools aren't already installed. In this case, Core Tools are installed the first time you run the app. Python version Python 3.7, 3.8, 3.9, or 3.10 Visual Studio Code will create a virtual environment with the version you select. Select how you would like to open your project Open in current window Reopens Visual Studio Code in the folder you selected.
::: zone-end
Visual Studio Code installs the Azure Functions Core Tools if needed. It also creates a function app project in a folder. This project contains the host.json and local.settings.json configuration files.
A requirements.txt file is also created in the root folder. It specifies the Python packages required to run your function app.
When you've created the project, the Azure Functions Visual Studio Code extension automatically creates a virtual environment with your selected Python version. You then need to activate the virtual environment in a terminal and install some dependencies required by Azure Functions and Durable Functions.
-
Open the requirements.txt in the editor and change its content to the following code:
azure-functions azure-functions-durable
-
Open the editor's integrated terminal in the current folder (Ctrl+Shift+`).
-
In the integrated terminal, activate the virtual environment in the current folder, depending on your operating system:
source .venv/bin/activate
source .venv/bin/activate
.venv\scripts\activate
-
In the integrated terminal where the virtual environment is activated, use pip to install the packages you defined.
python -m pip install -r requirements.txt
A basic Durable Functions app contains three functions:
- Orchestrator function: Describes a workflow that orchestrates other functions.
- Activity function: It's called by the orchestrator function, performs work, and optionally returns a value.
- Client function: It's a regular Azure Function that starts an orchestrator function. This example uses an HTTP triggered function.
::: zone pivot="python-mode-configuration"
You use a template to create the durable function code in your project.
-
In the command palette, search for and select
Azure Functions: Create Function...
. -
Follow the prompts and provide the following information:
Prompt Value Description Select a template for your function Durable Functions orchestrator Create a Durable Functions orchestration Provide a function name HelloOrchestrator Name of your durable function
You've added an orchestrator to coordinate activity functions. Open HelloOrchestrator/__init__.py to see the orchestrator function. Each call to context.call_activity
invokes an activity function named Hello
.
Next, you'll add the referenced Hello
activity function.
-
In the command palette, search for and select
Azure Functions: Create Function...
. -
Follow the prompts and provide the following information:
Prompt Value Description Select a template for your function Durable Functions activity Create an activity function Provide a function name Hello Name of your activity function
You've added the Hello
activity function that is invoked by the orchestrator. Open Hello/__init__.py to see that it takes a name as input and returns a greeting. An activity function is where you'll perform actions such as making a database call or performing a computation.
Finally, you'll add an HTTP triggered function that starts the orchestration.
-
In the command palette, search for and select
Azure Functions: Create Function...
. -
Follow the prompts and provide the following information:
Prompt Value Description Select a template for your function Durable Functions HTTP starter Create an HTTP starter function Provide a function name DurableFunctionsHttpStart Name of your client function Authorization level Anonymous For demo purposes, allow the function to be called without authentication
You've added an HTTP triggered function that starts an orchestration. Open DurableFunctionsHttpStart/__init__.py to see that it uses client.start_new
to start a new orchestration. Then it uses client.create_check_status_response
to return an HTTP response containing URLs that can be used to monitor and manage the new orchestration.
You now have a Durable Functions app that can be run locally and deployed to Azure. ::: zone-end
::: zone pivot="python-mode-decorators"
Version 2 of the Python programming model requires the following minimum versions:
- Azure Functions Runtime v4.16+
- Azure Functions Core Tools v4.0.5095+ (if running locally)
- azure-functions-durable v1.2.4+
The following application setting is required to run the v2 programming model:
- Name:
AzureWebJobsFeatureFlags
- Value:
EnableWorkerIndexing
If you're running locally using Azure Functions Core Tools, you should add this setting to your local.settings.json
file. If you're running in Azure, follow these steps with the tool of your choice:
Replace <FUNCTION_APP_NAME>
and <RESOURCE_GROUP_NAME>
with the name of your function app and resource group, respectively.
az functionapp config appsettings set --name <FUNCTION_APP_NAME> --resource-group <RESOURCE_GROUP_NAME> --settings AzureWebJobsFeatureFlags=EnableWorkerIndexing
Replace <FUNCTION_APP_NAME>
and <RESOURCE_GROUP_NAME>
with the name of your function app and resource group, respectively.
Update-AzFunctionAppSetting -Name <FUNCTION_APP_NAME> -ResourceGroupName <RESOURCE_GROUP_NAME> -AppSetting @{"AzureWebJobsFeatureFlags" = "EnableWorkerIndexing"}
- Make sure you have the Azure Functions extension for VS Code installed
- Press F1 to open the command palette. In the command palette, search for and select
Azure Functions: Add New Setting...
. - Choose your subscription and function app when prompted
- For the name, type
AzureWebJobsFeatureFlags
and press Enter. - For the value, type
EnableWorkerIndexing
and press Enter.
To create a basic Durable Functions app using these 3 function types, replace the contents of function_app.py
with the following Python code.
import azure.functions as func
import azure.durable_functions as df
myApp = df.DFApp(http_auth_level=func.AuthLevel.ANONYMOUS)
# An HTTP-Triggered Function with a Durable Functions Client binding
@myApp.route(route="orchestrators/{functionName}")
@myApp.durable_client_input(client_name="client")
async def http_start(req: func.HttpRequest, client):
function_name = req.route_params.get('functionName')
instance_id = await client.start_new(function_name)
response = client.create_check_status_response(req, instance_id)
return response
# Orchestrator
@myApp.orchestration_trigger(context_name="context")
def hello_orchestrator(context):
result1 = yield context.call_activity("hello", "Seattle")
result2 = yield context.call_activity("hello", "Tokyo")
result3 = yield context.call_activity("hello", "London")
return [result1, result2, result3]
# Activity
@myApp.activity_trigger(input_name="city")
def hello(city: str):
return "Hello " + city
Review the table below for an explanation of each function and its purpose in the sample.
Method | Description |
---|---|
hello_orchestrator |
The orchestrator function, which describes the workflow. In this case, the orchestration starts, invokes three functions in a sequence, and returns the ordered results of all 3 functions in a list. |
hello |
The activity function, which performs the work being orchestrated. The function returns a simple greeting to the city passed as an argument. |
http_start |
An HTTP-triggered function that starts an instance of the orchestration and returns a check status response. |
Note
Durable Functions also supports Python V2's blueprints. To use them, you will need to register your blueprint functions using the azure-functions-durable
Blueprint
class, as
shown here. The resulting blueprint can then be registered as normal. See our sample for an example.
::: zone-end
Azure Functions Core Tools lets you run an Azure Functions project on your local development computer. If you don't have it installed, you're prompted to install these tools the first time you start a function from Visual Studio Code.
::: zone pivot="python-mode-configuration"
- To test your function, set a breakpoint in the
Hello
activity function code (Hello/__init__.py). Press F5 or selectDebug: Start Debugging
from the command palette to start the function app project. Output from Core Tools is displayed in the Terminal panel.
::: zone-end
::: zone pivot="python-mode-decorators"
- To test your function, set a breakpoint in the
hello
activity function code. Press F5 or selectDebug: Start Debugging
from the command palette to start the function app project. Output from Core Tools is displayed in the Terminal panel.
::: zone-end
Note
For more information on debugging, see Durable Functions Diagnostics.
-
Durable Functions require an Azure storage account to run. When Visual Studio Code prompts you to select a storage account, select Select storage account.
:::image type="content" source="media/quickstart-python-vscode/functions-select-storage.png" alt-text="Screenshot of how to create a storage account.":::
-
Follow the prompts and provide the following information to create a new storage account in Azure:
Prompt Value Description Select subscription name of your subscription Select your Azure subscription Select a storage account Create a new storage account Enter the name of the new storage account unique name Name of the storage account to create Select a resource group unique name Name of the resource group to create Select a location region Select a region close to you -
In the Terminal panel, copy the URL endpoint of your HTTP-triggered function.
:::image type="content" source="media/quickstart-python-vscode/functions-f5.png" alt-text="Screenshot of Azure local output.":::
-
Use your browser, or a tool like Postman or cURL, send an HTTP request to the URL endpoint. Replace the last segment with the name of the orchestrator function (
HelloOrchestrator
). The URL must be similar tohttp://localhost:7071/api/orchestrators/HelloOrchestrator
.The response is the initial result from the HTTP function letting you know the durable orchestration has started successfully. It isn't yet the end result of the orchestration. The response includes a few useful URLs. For now, let's query the status of the orchestration.
-
Copy the URL value for
statusQueryGetUri
, paste it in the browser's address bar, and execute the request. Alternatively, you can also continue to use Postman to issue the GET request.The request will query the orchestration instance for the status. You must get an eventual response, which shows the instance has completed and includes the outputs or results of the durable function. It looks like:
::: zone pivot="python-mode-configuration"
{
"name": "HelloOrchestrator",
"instanceId": "9a528a9e926f4b46b7d3deaa134b7e8a",
"runtimeStatus": "Completed",
"input": null,
"customStatus": null,
"output": [
"Hello Tokyo!",
"Hello Seattle!",
"Hello London!"
],
"createdTime": "2020-03-18T21:54:49Z",
"lastUpdatedTime": "2020-03-18T21:54:54Z"
}
::: zone-end ::: zone pivot="python-mode-decorators"
{
"name": "hello_orchestrator",
"instanceId": "9a528a9e926f4b46b7d3deaa134b7e8a",
"runtimeStatus": "Completed",
"input": null,
"customStatus": null,
"output": [
"Hello Tokyo!",
"Hello Seattle!",
"Hello London!"
],
"createdTime": "2020-03-18T21:54:49Z",
"lastUpdatedTime": "2020-03-18T21:54:54Z"
}
::: zone-end
- To stop debugging, press Shift+F5 in Visual Studio Code.
After you've verified that the function runs correctly on your local computer, it's time to publish the project to Azure.
[!INCLUDE functions-create-function-app-vs-code]
[!INCLUDE functions-publish-project-vscode]
::: zone pivot="python-mode-configuration"
-
Copy the URL of the HTTP trigger from the Output panel. The URL that calls your HTTP-triggered function must be in this format:
http://<functionappname>.azurewebsites.net/api/orchestrators/HelloOrchestrator
::: zone-end ::: zone pivot="python-mode-decorators" -
Copy the URL of the HTTP trigger from the Output panel. The URL that calls your HTTP-triggered function must be in this format:
http://<functionappname>.azurewebsites.net/api/orchestrators/hello_orchestrator
::: zone-end -
Paste this new URL for the HTTP request in your browser's address bar. You must get the same status response as before when using the published app.
You have used Visual Studio Code to create and publish a Python durable function app.
[!div class="nextstepaction"] Learn about common durable function patterns