Managing VirtualMachines is often done using Azure Automation or PowerShell. But what if you want to do this using C#, in an Azure Function? This post shows a very simple example on how to implement this in a .Net Core 2 Azure Function.
Setting up the project
To get started, simply create an Azure Function project in Visual Studio:
Setting up the function
For this example I’m going to create a Function with HTTP trigger that deallocates all VMs in a resource group.
To get started, create a new function called ‘DeallocateVMs’:
I’ve chosen the ‘HTTP’ trigger because this is the easiest to run and debug on your local machine.
Setting up the packages
First of all, install the next NuGet packages:
Install-Package Microsoft.Azure.Management.Fluent Install-Package Microsoft.Azure.Management.Compute.Fluent
This allows us to make use of the Azure libraries we need to manage VMs.
Boilerplate code
The library needs to connect to Azure and have rights in the resource group you want to manage. For this, we need some boilerplate code to get the AzureCredentials :
internal static AzureCredentials GetCredentials() { var clientId = "Enter ClientId Here"; var clientSecret = "Enter ClientSecret Here"; var tenantId = "Enter TenantId Here"; return SdkContext.AzureCredentialsFactory.FromServicePrincipal(clientId, clientSecret, tenantId, AzureEnvironment.AzureGlobalCloud); }
For more info on creating Service Principals, please check out this link at Microsoft
After getting the credentials, the client can be configured:
var azure = Azure .Configure() .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic) .Authenticate(credentials) .WithDefaultSubscription();
Getting all VMs in a resource group
The IAzure interface allows us to list VMs, get information, and a lot more. Please check the recap of the blogpost for links to documentation.
In this example we want to deallocate all VMs in a specific resource group. I’ve skipped all checks and exception handling for sake of readability:
var resourcegroup = "your-resourcegroup-name-here"; foreach (var virtualMachine in await azure.VirtualMachines.ListByResourceGroupAsync(resourcegroup)) { log.LogInformation($"Deallocating VM {virtualMachine.Name}..."); await virtualMachine.DeallocateAsync(); log.LogInformation($"Deallocated VM {virtualMachine.Name}"); }
That is basically it! This function will now iterate thru all virtualmachines in the resource group and deallocate them
Recap
This example does not take into account what happens when you have very large numbers of virtual machines. The code also does not take into account what the current PowerState of the machine is. In a more advanced scenario, you should check the PowerState and determine what to do. The easiest way is to check if the machine is actually running:
if (virtualMachine.PowerState == PowerState.Running) { // Do something }
I’ve used a service principal with clientid and secrets, and set the rights on the managed resource groups. This is a very basic setup to show how to manage the VMs. There are alot of ways to implement security, and maybe your scenario has client certificates or other authentication options. This post on Github shows many scenarios on authenticating with this library.
Links
For this example, I’ve used the namespaces Microsoft.Azure.Management.Compute.Fluent and Microsoft.Azure.Management.Fluent Namespace. There are a lot of exampleson Github using the two NuGet packages.
Top comments (0)