Durable Functions is part of Azure Functions offering. Durable Functions helps you easily orchestrate stateful logic, making it an excellent solution for workflow scenarios, as well as stateful patterns like fan-out/fan-in and workloads that require long-running operations or need to wait arbitrarily long for external events.
This sample shows how to implement an order processing workflow with Durable Functions in Python 3.11. The sample leverages the Azure Developer CLI (azd) and Bicep to simplify the provisioning and deployment of all resources required by the app.
Durable Functions needs a storage backend provider to persist application states. This sample uses the default backend, which is Azure Storage.
Important
This sample creates several resources. Make sure to delete the resource group after testing to minimize charges!
The project is designed to run on your local computer, provided you have met the required prerequisites. You can run the project locally in these environments:
-
Start Azurite storage emulator. See this page for how to configure and start the Azurite emulator for Local Storage.
-
Clone the repo, then create a file named
local.settings.json
with the following content in the order_processor directory:{ "IsEncrypted": false, "Values": { "AzureWebJobsStorage": "UseDevelopmentStorage=true", "FUNCTIONS_WORKER_RUNTIME": "python" } }
Make sure Azurite is started before proceeding.
- Open the cloned repo in a new terminal and navigate to the
order_processor
directory:
cd order_processor
- Create and activate the virtual environment:
python3 -m venv venv_name
source .venv/bin/activate
- Install required packages:
python3 -m pip install -r requirements.txt
- Start function app
func start
- This sample uses an HTTP trigger to start an orchestration, so open a browser and go to http://localhost:7071/api/orchestrators/process_orchestrator. You should see something similar to the following:
{
"id": "e838bdb52db24560a6b30c261ac2985d",
"purgeHistoryDeleteUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/e838bdb52db24560a6b30c261ac2985d?code=<code>",
"sendEventPostUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/e838bdb52db24560a6b30c261ac2985d/raiseEvent/{eventName}?code=<code>",
"statusQueryGetUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/e838bdb52db24560a6b30c261ac2985d?code=<code>",
"terminatePostUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/e838bdb52db24560a6b30c261ac2985d/terminate?reason={{text}}}&code=<code>"
}
- To check the status of the orchestration instance started, go to the
statusQueryGetUri
. Your orchestration instance should show status "Running". After a few seconds, refresh to see that the orchestration instance is "Completed" and what the output is.
{
"name": "process_orchestrator",
"instanceId": "5ddc571338594942a5051903e4a50656",
"runtimeStatus": "Completed",
"input": "{\"__class__\": \"OrderPayload\", \"__module__\": \"models\", \"__data__\": \"{\\\"order_name\\\": \\\"milk\\\", \\\"total_cost\\\": 5, \\\"quantity\\\": 1}\"}",
"customStatus": "Order placed successfully.",
"output": {
"success": true
},
"createdTime": "2025-01-15T01:41:18Z",
"lastUpdatedTime": "2025-01-15T01:41:39Z"
}
- Open the order_processor folder in a new terminal
- Open VS Code by entering
code .
in the terminal - Start Azurite by opening the command template and searching for
Azurite: Start
- Press Run/Debug (F5) to run in the debugger
- Use same approach above to start an orchestration instance and check its status.
- Login to your Azure account.
azd auth login
- In the root folder (durable-functions-order-processing-python) use the Azure Developer CLI (azd) to provision a new resource group with the environment name you provide and all the resources for the sample by running:
azd up
Once the deployment is done, inspect the new resource group. The Durable Functions app, Flex Consumption hosting plan, Azure Storage account, App Insights, managed identity, and various networking related resources have been created and configured.
Because Durable Functions requires access to Azure Storage Blob, Table, and Queue, the associated networking resources such as private endpoints, link, etc. are created for each of those. The managed identity assigned to the app is also given the required role-based access control (RBAC) permissions.
Following guidance for hosting Durable Functions apps in the Flex Consumption plan, this sample sets the plan's always ready instance to 1 and the property maxQueuePollingInterval
to 1 second.
-
Use this Function CLI command to quickly find the function app url:
func azure functionapp list-functions <APP_NAME> --show-keys
The url should look something like this: https://func-processor-abcdefgxzy.azurewebsites.net/api/orchestrators/process_orchestrator.
Or you can find the app url on Azure portal.
-
Open up a browser and go that url to trigger the start of an orchestration instance.
-
Go to the
statusQueryGetUri
to see the status of your orchestration instance. It should show "Running" (or "Pending" then "Running") and change to "Completed" after a few seconds if you refresh the page. The result should be similar to above.
When you no longer need the resources created in this sample, run the following command to delete the Azure resources:
azd down
For more information on Durable Functions or the new Flex Consumption plan, see the following resources: