.netazurevirtual-machineiaas

Detect whether running on Azure IaaS VM (.NET Application)


I have seen many posts relating to what I believe should be a relatively simple task. As part of our licensing criteria we detect if we are running within a virtualised environment. However some identifiers we were using can change as a result of deallocation of the VM’s and so we started looking into substituting some of these with the Azure Unique ID (UUID). But in order to do we so we need to make changes to our licensing code to correctly detect we are running in an azure environment. I’ve seen many posts relating to using the Azure SDK and RoleEnvironment, but I believe this doesn’t work when running on managed IaaS VM’s (When copying the requisite DLL’s across).

So ideally we want a way within a .NET application without having dependencies on the Azure SDK that we can correctly identify an Azure VM as opposed to a standard VM. This could include registry keys, WMI lookups etc.

I will note an existing solution found on the technet website of microsoft Script Detect Windows Azure Virtual Machine, but because the code we are running is generalised to work for all types of environments this isn't really practical.


Solution

  • The Azure Instance Metadata Service provides information about running virtual machine instances that can be used to manage and configure your virtual machines. This includes information such as SKU, network configuration, and upcoming maintenance events. For more information on what type of information is available, see metadata categories.

    Azure's Instance Metadata Service is a REST Endpoint accessible to all IaaS VMs created via the Azure Resource Manager. The endpoint is available at a well-known non-routable IP address 169.254.169.254 that can be accessed only from within the VM.

    https://learn.microsoft.com/en-us/azure/virtual-machines/windows/instance-metadata-service