Home > other >  Creating Azure VM via C# Throws Error While Creating Resource Group
Creating Azure VM via C# Throws Error While Creating Resource Group

Time:09-28

I'm attempting to create a VM programmatically...actually, following an example in a book. Before running the program I went ahead and created an Azure AD application and service principal via the portal enter image description here

CodePudding user response:

I resolved the issue by replacing AzureCredentialsFactory.FromFile with AzureCredentialsFactory.FromServicePrincipal. Thanks ShrutiJoshi-MT for the input. I simply created a json file with the necessary credentials.

I still had some issues related to authorization. It turns out I didn't give the App Service appropriate authorization level. This post helped resolve that issue: The client with object id does not have authorization to perform action 'Microsoft.DataFactory/datafactories/datapipelines/read' over scope.

Final code:

string jsonString = File.ReadAllText("../../../azureauth.json");
AuthItem authItem = JsonSerializer.Deserialize<AuthItem>(jsonString);
var credentials = SdkContext.AzureCredentialsFactory
    .FromServicePrincipal(authItem.ClientId, authItem.SecretValue, authItem.TenantId, AzureEnvironment.AzureGlobalCloud);

//Create the management client. This will be used for all the operations we will perform in Azure.
var azure = Azure.Configure().WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic).Authenticate(credentials).WithSubscription(authItem.Subscription);

//Create a resource group
var groupName = "az204-ResourceGroup";
var vmName = "az204VMTesting";
var location = Region.USEast;
var vNetName = "az204VNET";
var vNetAddress = "172.16.0.0/16";
var subnetName = "az204Subnet";
var subnetAddress = "172.16.0.0/24";
var nicName = "az204NIC";
var adminUser = "azureadminuser";
var adminPassword = "Pa$$w0rd!2019";

    Console.WriteLine($"Creating resource group {groupName} ... ");
    var resourceGroup = azure.ResourceGroups.Define(groupName).WithRegion(location).Create();

    //Every VM needs to be connected to a virtual network
    Console.WriteLine($"Creating virtual network {vNetName} ...");
    var network = azure.Networks.Define(vNetName)
        .WithRegion(location)
        .WithExistingResourceGroup(groupName)
        .WithAddressSpace(vNetAddress)
        .WithSubnet(subnetName, subnetAddress)
        .Create();

    //Any VM needs a network interface for connecting to the virtual network
    Console.WriteLine($"Creating network interface {nicName} ... ");
    var nic = azure.NetworkInterfaces.Define(nicName)
        .WithRegion(location)
        .WithExistingResourceGroup(groupName)
        .WithExistingPrimaryNetwork(network)
        .WithSubnet(subnetName)
        .WithPrimaryPrivateIPAddressDynamic()
        .Create();

    //Create the VM
    Console.WriteLine($"Creating VM {vmName} ... ");
    azure.VirtualMachines.Define(vmName)
        .WithRegion(location)
        .WithExistingResourceGroup(groupName)
        .WithExistingPrimaryNetworkInterface(nic)
        .WithLatestWindowsImage("MicrosoftWindowsServer", "WindowsServer", "2012-R2-Datacenter")
        .WithAdminUsername(adminUser)
        .WithAdminPassword(adminPassword)
        .WithComputerName(vmName)
        .WithSize(VirtualMachineSizeTypes.StandardDS2V2)
        .Create();
  • Related