Creating Durable Azure Functions

30 minutes
  • 5 Learning Objectives

About this Hands-on Lab

In this lab, we create and run an Azure Durable Function and retrieve the result of the orchestration via PowerShell.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Log In to the Azure Portal

Log in to the Azure Portal using the username and password supplied by the lab.

  1. Open a browser.
  2. Navigate to the provided Azure Portal URL.
  3. Use the supplied username and password to authenticate.
Add a Durable Orchestration Function to the Function App

Go to the overview of the provided function application service. Starting in the Azure dashboard perform the following tasks:

  1. Open the navigation menu in the upper-left of the Portal.
  2. Click All resources. Wait for the all resources list to appear.
  3. In the list, click on the app service with a name starting with fa-. This will open the overview page of the function app service.
  4. Click the Functions menu item in the navigation tree on the left of the page.
  5. Click New function near the top center of the page.
  6. From the list of trigger types, click Durable Functions orchestrator.
  7. In the New Function panel that appears on the right of the page, enter "MyDurableFunctionsOrchestrator" as the name.
  8. Click Create.
  9. Wait for the function to appear.
Add a Durable Activity Function to the Function App

Now create the Durable Functions activity function:

  1. Click the Functions menu item in the navigation tree on the left of the page.
  2. Click New function near the top center of the page.
  3. From the list of trigger types, click Durable Functions activity.
  4. In the New Function panel the appears on the right of the page, enter "Hello" as the name.
  5. Click Create.
  6. Wait for the function to appear.
Create a Durable Functions HTTP Starter Function in the Function App

Now create the Durable Functions HTTP starter:

  1. Click the Functions menu item in the navigation tree on the left of the page.
  2. Click New function near the top center of the page.
  3. From the list of trigger types, click Durable Functions HTTP starter.
  4. In the New Function panel the appears on the right of the page, enter "MyDurableFunctionsHttpStart" as the name.
  5. Click Create.
  6. Wait for the function to appear.

Now change the source code of the function to the following, which removes passing data from the request to the orchestrator function:

#r "Microsoft.Azure.WebJobs.Extensions.DurableTask"
#r "Newtonsoft.Json"

using System.Net;
using Microsoft.Azure.WebJobs.Extensions.DurableTask;

public static async Task<HttpResponseMessage> Run(
    HttpRequestMessage req,
    IDurableOrchestrationClient starter,
    string functionName,
    ILogger log)
{
    // Function input comes from the request content.
    //dynamic eventData = await req.Content.ReadAsAsync<object>();

    // Pass the function name as part of the route 
    string instanceId = await starter.StartNewAsync(functionName, null);

    log.LogInformation($"Started orchestration with ID = '{instanceId}'.");

    return starter.CreateCheckStatusResponse(req, instanceId);
}

Click Save above the code to save this new code.

Run the Durable Function Starter and Get the Results with Azure CLI
  1. Get the URL of the Durable HTTP start function by clicking </> Get function URL above the MyDurableFunctionsHttpStart code and copy the URL to the clipboard.

  2. Open an Azure CloudShell by clicking the CloudShell icon at the top of the page (it looks like a rectangle with a command line prompt within it).

  3. When the Welcome to Azure Cloud Shell panel opens at the bottom of the browser, click PowerShell.

  4. A You have to storage mounted form will open. In this, select Create new in the File share section. For the name of the share, enter "files". Then click on the Create storage button.

    The panel will become blue and the shell will take a moment to initialize.

    When the prompt is available, run the following commands one at a time:

    $startUrl = "<url from the Durable HTTP starter function>"

    Make sure to replace <url from the Durable HTTP starter function> with the actual URL copied in a previous step. Now start the orchestration:

    $r = Invoke-WebRequest -Uri $startUrl

    Parse the content in the result of the orchestration invocation:

    $response = ConvertFrom-Json $r.Content

    Now retrieve the status of the orchestration:

    $s = Invoke-WebRequest -Uri $response.statusQueryGetUri

    Parse the content of the status:

    $status = ConvertFrom-Json $s.Content

    And finally retrieve the output of the orchestration:

    $status.output

Additional Resources

Your employer needs to create a set of Azure Functions that run in a specific sequence and which will require persisting state from one function to another. They will likely need human interaction to get an approval or refusal at some point in their execution. You tell them this is the perfect case for Azure Durable Functions. In response, they ask you to demonstrate how to code and run a simple durable function orchestration.

When setting up the cloud shell, please ensure to set the cloud shell region to the same location as the existing storage account.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?