Use Azure Functions to extend DevTest Labs
You can use Azure Functions to support more scenarios beyond the ones that DevTest Labs already supports. You can use Azure Functions to extend the built-in functionality of the service to meet your business-specific needs. The following list provides some of the possible scenarios. This article shows you how to implement one of these sample scenarios.
- Provide a top-level summary of virtual machines (VMs) in the Lab
- Configure a lab to use a remote desktop gateway
- Compliance reporting on the internal support page
- Enable users to complete operations that require increased permissions in the subscription
- Starting workflows based on DevTest Labs events
Azure Functions is a serverless computing platform in Azure. Using Azure Functions in a solution with DevTest Labs enables us to augment the existing features with our own custom code. For more information on Azure Functions, see Azure Functions documentation. To illustrate how Azure Functions can help fulfill your requirements or complete scenarios in DevTest Labs, this article uses an example of providing a top-level summary of VMs in the lab as follows:
Example requirement/scenario: Users can see details about all VMs in a lab including the operating system, owner, and any applied artifacts. Also, if the Apply Windows Updates artifact hasn't been recently applied, there's an easy way to apply it.
To complete the scenario, you use two functions, as described in the following diagram:
The source code for these sample functions is located in the DevTest Labs GitHub repository (both C# and PowerShell implementations are available).
- UpdateInternalSupportPage: This function queries DevTest Labs and updates the internal support page directly with details about the virtual machines.
- ApplyWindowsUpdateArtifact: For a VM in a lab, this function applies the Windows update artifact.
How it works
When users select the Internal support page in DevTest Labs, they have a pre-populated page with information about VMs, lab owners, and support contacts.
When you select the Select here to refresh button, the page calls the first Azure function: UpdateInternalSupportPage. The function queries DevTest Labs for information and then rewrites the Internal support page with the new information.
There's another action you can take. For any VMs that haven't had Windows Update artifacts applied recently, there's a button to apply windows updates to the VM. When you select the *Run Windows update button for a VM, the page calls the second Azure Function: ApplyWindowsUpdateArtifact. This function checks whether the virtual machine is running and if so, applies the Windows Update artifact directly.
This section provides step-by-step instructions for setting up Azure Resources needed to update the Internal support page. This walkthrough provides one example of extending DevTest Labs. You can use this pattern for other scenarios.
Step 1: Create a service principal
The first step is to get a service principal with permission to the subscription that contains the lab. The service principal must use the password-based authentication. It can be done with Azure CLI, Azure PowerShell, or the Azure portal. If you already have a service principal to use, you can skip this step.
Note down the application ID, key, and tenant ID for the service principal. You need them later in this walkthrough.
Step 2: Download the sample and open in Visual Studio 2019
Open the sample solution with Visual Studio 2019.
Install the Azure development workload for Visual Studio if you don't have it already installed. It can be installed via Tools -> Get Tools and Features menu item).
Build the solution. Select Build and then Build Solution menu item.
Step 3: Deploy the sample to Azure
In Visual Studio, In the Solution Explorer window, right-click the AzureFunctions project, and then select Publish. Follow the wizard to complete publishing to either a new or an existing Azure Function App. For detailed information on developing and deploying Azure functions using Visual Studio, see Develop Azure Functions using Visual Studio.
Step 4: Gather application settings
Once the functions are published, you need to get URLs for these functions from the Azure portal.
Navigate to the Azure portal.
Find the function app.
On the Function Apps page, select the function.
Select Get function URL as shown in the following image.
Copy and save the URL. Repeat these steps for the other Azure function.
You also need information about the service principal, such as application ID, key, and tenant ID.
Step 5: Update Application Settings
In Visual Studio, after publishing the Azure Function, select the Edit Azure App Service Settings under Actions. Update the following application settings (remote):
WindowsUpdateAllowedDays (default to 7)
Step 6: Test the Azure function
The last step in this walkthrough is to test the Azure function.
Navigate to the UpdateInternalSupportPage function in the function app created in the step 3.
Select Test on the right side of the page.
Enter in the route properties (LABNAME, RESOURCEGROUPNAME, and SUBSCRIPTIONID).
Select Run to execute the function.
This function will update the internal support page of the specified lab. It also includes a button for users to directly call the function next time
Azure Functions can help extend the functionality of DevTest Labs beyond what’s already built-in and help customers meet their unique requirements for their teams. This pattern can be extended & expanded further to cover even more. To learn more about DevTest Labs, see the following articles: