Locating Azure Windows Virtual Machine Images for Terraform

Creating a Terraform module that deploys a single Windows virtual machine (VM) is relatively straightforward. However, it is a little convoluted to locate the VM Image information. This blog covers procedures that allow you to find the VM image information needed to deploy a particular version of a virtual server or workstation. The Terraform main.tf file contains a resource block called “azurerm_windows_virtual_machine.” This resource block contains information needed to build the VM in Azure. Below is an example of minimal information required to create a VM using Terraform.

Figure 1

A great deal more coding could be added, but that is not the purpose of this blog. It is included for clarity. We will stick to finding the “source_image_reference” information. The information required to deploy a Windows virtual machine is:

  1. Publisher: The organization that created the image such as “MicrosoftWindowsServer” , MicrosoftWindowsDesktop
  2. Offer: The name of a group of related images created by a publisher such as “UbuntuServer”
  3. SKU: An instance of an offer, such as a major release of distribution such as “2022-datacenter-azure-edition”.
  4. Version: The version number of an image SKU. Most of the time you would just put “latest”

You can use PowerShell on your Windows 11/10 pc, but you will have to load the az.compute PowerShell package from the PowerShell gallery to access the Cmdlets, and you will need to connect to your Azure subscription. We will do it the easy way by using CloudShell built into the Azure Portal, and we will focus on finding the information needed to deploy Windows Server 2022 Azure Edition.

Lets find the image information for Windows Server 2022 Azure Edition

Step 1:  Log into your Azure Portal and open CloudShell

Step 2: Find the Publisher

  • Set the region location by setting the location variable in PowerShell:

$location  =  “usgovvirginia”  (or whatever region you are using)

  • List the publishers for the region. This command scrolls all publishers to the screen:

Get-AzVMImagePublisher -Location $location | Select PublisherName

Figure 2
  • If you want to pipe all Publishers to a file:            

Get-AzVMImagePublisher -Location $location | Select PublisherName | Out-File -FilePath AzurePublisherList.txt

  • To find the number of publishers, count the lines in the text file you just created:

Get-Content AzurePublisherList.txt | measure -Line -Character -Word

Figure 3
  • To find Publishers containing “Windows” use:

Get-AzVMImagePublisher -Location $location | Select PublisherName | Where-Object { $_.PublisherName -like ‘*Windows*’ }

The Windows Server 2022 publisher is “MicrosoftWindowsServer”

Figure 4

Step 3: Find the “offer”:

  • Set the Publisher variable by entering:

$publisher =  “MicrosoftWindowsServer”    

  • To list the Offer’s enter:

 Get-AzVMImageOffer -Location $location -PublisherName $publisher | Select Offer

The Windows Offer is “WindowsServer”.

Figure 5

Step 4: Find the SKU:

  • Set the “offer” variable by entering:

$offer =  “WindowsServer” 

  • To Get the SKU for the offer enter:

Get-AzVMImageSku -Location $location -PublisherName $publisher -Offer $offer | Select

Figure 6
  • Search through the output and find the Windows Server 2022 Azure edition SKU.

At the end of the process, you have:

                Publisher: MicrosoftWindowsServer
                Offer: WindowsServer
                SKU: 2022-datacenter-azure-edition
                Version: latest

This is the manual method of finding the VM image information. I am sure sharp administrators out there can bundle this information into a script. Or you could pipe the output of Get-AzVMImageSku Cmdlet to a text file and import it into an excel spreadsheet. There are several ways to export and save this information. However, I’d be careful about keeping and referring to “old” information. Microsoft does seem to change things quite often. At a minimum, I would save the variables for future use. I hope this post is helpful. You can find the basis of this blog here.

Author: Chet Camlin

Leave a Reply