Search code examples
c#azureazure-marketplace

Raise Azure VM from marketplace image via C#


Failing to raise an azure VM from a marketplace image programatically.

The code:

var linuxVM = await _azure.VirtualMachines.Define(linuxVmName)
          .WithRegion(Region)
          .WithExistingResourceGroup(rgName)
          .WithNewPrimaryNetwork("10.0.0.0/28")
          .WithPrimaryPrivateIPAddressDynamic()
          .WithoutPrimaryPublicIPAddress()
          .WithSpecificLinuxImageVersion(new ImageReference())
          .WithRootUsername(userName)
          .WithRootPassword(password)
          .WithSize(VirtualMachineSizeTypes.StandardNC6sV3)
          .WithPlan(new PurchasePlan("nvidia", "ngc-base-version-20-10-1", "ngc_azure_17_11"))
          .CreateAsync();

In Azure I've enabled "Want to deploy programmatically? Get started" for the given image (as explained here).

There are several options as to the method that selects the image, not sure which method should be used and with which parameters. Tried several combinations, but all returned misc error messages.

Did not find code samples more detailed this (which does not explain how to use an image from the marketplace).


Edit:

The code above returns this exception:

Microsoft.Rest.Azure.CloudException: 'This resource was created without a plan. A new plan cannot be associated with an update.'

Another attempt with more populated parameters causes the same exception:

.WithSpecificLinuxImageVersion(new ImageReference(new ImageReferenceInner(
                          publisher: "nvidia",
                          offer: "ngc_azure_17_11",
                          sku: "ngc-base-version-20-10-1"
                          )))

Solution

  • The missing parameter was the image's version. The code to raise the image looks like so:

        var vm = await _azure.VirtualMachines.Define(linuxVmName)
              .WithRegion(_region)
              .WithExistingResourceGroup(_rgName)
              .WithNewPrimaryNetwork("10.0.0.0/28")
              .WithPrimaryPrivateIPAddressDynamic()
              .WithoutPrimaryPublicIPAddress()
              .WithSpecificLinuxImageVersion(new ImageReference(new ImageReferenceInner(
                  publisher: "nvidia",
                  offer: "ngc_azure_17_11",
                  sku: "ngc-base-version-20-10-1",
                  version: "20.10.1"
                  )))
              .WithRootUsername(userName)
              .WithRootPassword(password)
              .WithSize(VirtualMachineSizeTypes.StandardNC6sV3)
              .WithPlan(new PurchasePlan("nvidia", "ngc-base-version-20-10-1", "ngc_azure_17_11"))
              .CreateAsync();
    

    The version can be found in the UI: enter image description here

    It's also possible to get all the image's details via CLI:

    Get-AzVMImageOffer -Location "West Europe" -PublisherName nvidia
    

    A fuller guide can be found here