Edit

Share via


Use Azure Functions to extend Azure DevTest Labs

Azure Functions is a serverless computing platform in Azure. You can use Functions to support extra scenarios in Azure DevTest Labs. When you use Functions in a solution with DevTest Labs, you can augment the existing service features with your own custom code.

This article shows how to extend DevTest Labs built-in functionality to provide a top-level summary of virtual machines (VMs) in a lab. Here are some other scenarios you can support by extending DevTest Labs with Functions:

The following sections provide step-by-step instructions for setting up the Azure Resources needed to update the Internal support page in DevTest Labs. This walkthrough provides one example for how to extend DevTest Labs. You can use this pattern for other scenarios.

Explore sample scenario

The sample walkthrough in this article extends DevTest Labs by using Functions to support the following features:

  • Provide a top-level summary of all VMs in a lab
  • Enable lab users to see virtual machine (VM) details, including operating system, owner, and applied artifacts
  • Offer a quick way to apply the Windows Update artifact

The scenario implements two Azure functions:

  • UpdateInternalSupportPage: Queries DevTest Labs and updates the internal support page directly with details about the lab VMs.
  • ApplyWindowsUpdateArtifact: Applies the Windows update artifact to all VMs in the lab, as needed.

The following diagram demonstrates the overall flow of function behavior in the scenario:

Diagram that demonstrates the overall flow to extend Azure DevTest Labs with Azure Functions for the sample scenario.

  • When the user browses to the Internal support page in DevTest Labs, they see a prepopulated page with information about lab VMs, lab owners, and support contacts.

  • When the user selects the Select here to refresh option, the page calls the Azure function UpdateInternalSupportPage. This function queries DevTest Labs for information and then refreshes the Internal support page with the latest information.

  • When a VM doesn't have a recent Windows update artifact, the user can select the *Run Windows update option for the VM. The page calls the Azure Function ApplyWindowsUpdateArtifact. This function checks whether the VM is running, and if so, applies the Windows Update artifact directly.

Prerequisites

  • To work with the sample in this walkthrough, you need a service principal that has permission to the subscription that contains the lab. The service principal must use password-based authentication. If you already have a service principal that you can use in this walkthrough, you can continue to the next section.

    1. To get the service principal, you can use the Azure CLI, Azure PowerShell, or the Azure portal.

    2. After you have the service principal, copy and save the application ID, key, and tenant ID values. You use the values later in the walkthrough.

Download sample and build solution in Visual Studio

The source code for the sample is located in the DevTest Labs Internal Support Page integration with Azure Functions GitHub repository. The sample files support both C# and PowerShell implementations.

After you have the service principal, you're ready to get the sample source:

  1. Copy the C# Azure Functions sample to your local system. You can clone or download the Azure DevTest Labs Archive repository.

  2. Open the sample solution in Visual Studio.

  3. As needed, install the Azure development workload for Visual Studio by selecting Tools > Get Tools and Features:

    Screenshot that shows how to install the Azure development workload in Visual Studio.

  4. Select Build > Build Solution to complete the process.

Deploy sample to Azure

The next step is to deploy the sample Function application to Azure:

  1. In Visual Studio, open the Solution Explorer window.

  2. Right-click the AzureFunctions project, and then select Publish.

  3. Complete the Wizard steps to publish a new or existing Azure Function App:

    Screenshot that shows the Publish dialog for an Azure Function app in Visual Studio.

    For detailed information on developing and deploying Azure Functions applications in Visual Studio, see Develop Azure Functions by using Visual Studio.

Get function URL

After you publish the Function app, you need to get URLs for the functions from the Azure portal:

  1. In the Azure portal, go to the Function App page, and select the Function app that you created in Visual Studio.

  2. On the Overview page for the Function app, locate the Functions section and select one of the functions: UpdateInternalSupportPage or ApplyWindowsUpdateArtifact:

    Screenshot that shows how to access the Azure functions for the Function app in the Azure portal.

  3. On the Code + Test page for the Azure function, select Get function URL.

  4. On the Get Function URL pane, use the Copy action to copy the desired URL to your clipboard:

    Screenshot that shows how to copy the URL for the function in the Azure portal.

  5. Save the copied URL for later use.

  6. Repeat these steps for the other Azure function.

Update application settings

In addition to the URL for each function, you also need to update information about the service principal, including the application ID, key, and tenant ID.

Follow these steps to update the required application settings in Visual Studio:

  1. In Visual Studio, return to the Publish page for the AzureFunctions project.

  2. In the Hosting section, select More actions (...), and then select Manage Azure App Service settings:

    Screenshot that shows how to manage the Azure App Service settings from the Publish page for the Function app.

  3. On the Application settings dialog, update the Remote value for the following settings:

    • AzureFunctionUrl_ApplyUpdates
    • AzureFunctionUrl_UpdateSupportPage
    • WindowsUpdateAllowedDays (default to 7)
    • ServicePrincipal_AppId
    • ServicePrincipal_Key
    • ServicePrincipal_Tenant

    Screenshot that shows how to update the Azure App Service Function application settings in Visual Studio.

  4. Select OK.

Test Azure function

The last step is to test the Azure function:

  1. In the Azure portal, go to the Function App page, and select the Function app that you created in Visual Studio.

  2. On the Overview page for the Function app, locate the Functions section and select the UpdateInternalSupportPage function.

  3. On the Code + Test page for the Azure function, select Test / Run.

  4. On the Test / Run pane, enter values for the route properties: LABNAME, RESOURCEGROUPNAME, and SUBSCRIPTIONID:

    Screenshot that shows how to enter the property values for the function test.

  5. Select Run to execute the function. The function test updates the internal support page of the specified lab. It also includes an option for users to directly call the function next time.