Intro

In this post I will describe how to manage the state of a VM with Azure Data Factory (ADF). I was inspired to write this post after reading a very similar walk through on Tech Talk Corner

A VM can be in one of several states, for this post we are going to focus on Running and Deallocated. When a VM is running you are being billed for it and able to access its resources. If the VM is deallocated you are not being billed for it and its resources are inaccessible. In many scenarios it makes sense to have a VM in a deallocated state to save on cost and only keep it running when absolutely necessary.

Gather Information

To make this solution work we will need a few pieces of information from the VM. All of this information can be found on the Overview page of the VM in the Azure portal.

  1. Subscription ID
  2. Resource Group
  3. Region
  4. VM Name

Grant Permission

After gathering all of the information above from the Overview page navigate to the Access control (IAM) page. Here you will need to add your ADF managed identity to the Virtual Machine Contributor role.

Management Pipeline

There are several ways to implement this in ADF. I found the best way for me was to create a new pipeline that I can call with either the Start or Stop command. In this pipeline we will create a web activity that uses the Azure Compute REST API.

  1. Create a new pipeline. I named mine VM_StartStop for easy reference. Name yours whatever you would like.
  2. Create 5 parameters leaving the type set to String. Set all of the parameters except Command default value to the values gathered earlier.
    1. SubscriptionId
    2. ResourceGroupName
    3. Region
    4. VmName
    5. Command
  1. Create a new Web activity.
  2. In the Web activity settings update the following
    1. URL = @concat(‘https://management.azure.com/subscriptions/’,pipeline().parameters.SubscriptionId,’/resourceGroups/’,pipeline().parameters.ResourceGroupName,’/providers/Microsoft.Compute/virtualMachines/’,pipeline().parameters.VmName,’/’,pipeline().parameters.Command,’?api-version=2021-11-01‘)
    2. Method = POST
    3. Body = @concat(‘{‘,’}’)
    4. Authentication = System Assigned Managed Identity
    5. Resource = https://management.azure.com/

Integrating

Now that the management pipeline has been created we can call it from anywhere within our Data Factory. All you have to do is add an Execute Pipeline activity and pass either Start or Deallocate in the Command parameter.

Conclusion

We covered how to implement a VM management pipeline that can be called from any other pipeline to turn a VM on and off. The pattern I used was to call the management pipeline to start the VM, do the work I needed to do with the VM resources, then deallocate the VM. Using this pattern I am able to minimize cost for VM resources, especially those using expensive licensing like SQL Server Enterprise. For further reading I strongly recommend checking out the post on Tech Talk Corner which also covers how to check the status of the VM.