How to deploy custom VMs in Azure?

Kumar Allamraju
3 min readJun 29, 2020

--

Often times customers ask us about a particular custom VM version is not available in Azure Marketplace. My answer is it’s not possible for Azure to provide all flavors of Linux images and versions. However customers can upload their existing VHDs to Azure Storage account and build a custom VM out of that VHD file. The process is very simple, let me outline the steps here.

Pre-requisites

  • A valid Azure subscription
  • Azure PowerShell Az 1.0 or later

Steps to build a custom image

  1. I found an existing CentOS 7.7 at CentOS website
  2. Download the CentOS-7-x86_64-Azure-1907.vhd to your machine.
  3. Use the PowersShell script mentioned at https://docs.microsoft.com/en-us/azure/virtual-machines/scripts/virtual-machines-windows-powershell-upload-generalized-script . I have slightly modified the script to deploy Linux VM
Connect-AzAccount# Provide values for the variables$resourceGroup = 'myRG'
$location = 'WestUS'
$storageaccount = 'mystorage123'
$storageType = 'Standard_LRS'
$containername = 'mycontainer'
$localPath = 'C:\Users\kumara\Downloads\CentOS7.vhd'
$vmName = 'myCentOS7VM'
$imageName = 'myCentOS7Image2'
$vhdName = 'MyCentOS7.vhd'
$diskSizeGB = '128'
$subnetName = 'subnetA'
$vnetName = 'myvNet'
$ipName = 'myCentOSPip'
$nicName = 'myCentOSNic'
$nsgName = 'myNsg'
$ruleName = 'myRdpRule'
$computerName = 'myCentOS7VM'
$vmSize = 'Standard_DS1_v2'
# Upload the VHDNew-AzResourceGroup -Name $resourceGroup -Location $locationNew-AzStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccount -Location $location `-SkuName $storageType -Kind "Storage"$urlOfUploadedImageVhd = ('https://' + $storageaccount + '.blob.core.windows.net/' + $containername + '/' + $vhdName)Add-AzVhd -ResourceGroupName $resourceGroup -Destination $urlOfUploadedImageVhd -LocalFilePath $localPath# Note: Uploading the VHD may take awhile!# Create a managed image from the uploaded VHD$imageConfig = New-AzImageConfig -Location $location$imageConfig = Set-AzImageOsDisk -Image $imageConfig -OsType Linux -OsState Generalized -BlobUri $urlOfUploadedImageVhd$image = New-AzImage -ImageName $imageName -ResourceGroupName $resourceGroup -Image $imageConfig# Get the username and password to be used for the administrators account on the VM.This is used when connecting to the VM using RDP.$cred = Get-Credential# Create the networking resources$singleSubnet = New-AzVirtualNetworkSubnetConfig -Name $subnetName -AddressPrefix 10.0.0.0/24$vnet = New-AzVirtualNetwork -Name $vnetName -ResourceGroupName $resourceGroup -Location $location -AddressPrefix 10.0.0.0/16 -Subnet $singleSubnet$pip = New-AzPublicIpAddress -Name $ipName -ResourceGroupName $resourceGroup -Location $location -AllocationMethod Dynamic$rdpRule = New-AzNetworkSecurityRuleConfig -Name $ruleName -Description 'Allow SSH' -Access Allow -Protocol Tcp -Direction Inbound -Priority 110 -SourceAddressPrefix Internet -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 22$nsg = New-AzNetworkSecurityGroup -ResourceGroupName $resourceGroup -Location $location -Name $nsgName -SecurityRules $rdpRule$nic = New-AzNetworkInterface -Name $nicName -ResourceGroupName $resourceGroup -Location $location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $nsg.Id$vnet = Get-AzVirtualNetwork -ResourceGroupName $resourceGroup -Name $vnetName# Create a managed image from the uploaded VHD$imageConfig = New-AzImageConfig -Location $location$imageConfig = Set-AzImageOsDisk -Image $imageConfig -OsType Linux -OsState Generalized -BlobUri $urlOfUploadedImageVhd$image = New-AzImage -ImageName $imageName -ResourceGroupName $resourceGroup -Image $imageConfig# Start building the VM configuration$vm = New-AzVMConfig -VMName $vmName -VMSize $vmSize# Set the VM image as source image for the new VM$vm = Set-AzVMSourceImage -VM $vm -Id $image.Id# Finish the VM configuration and add the NIC.$vm = Set-AzVMOSDisk -VM $vm -DiskSizeInGB $diskSizeGB -Linux -CreateOption FromImage -Caching ReadWrite$vm = Set-AzVMOperatingSystem -VM $vm -Linux -ComputerName $computerName -Credential $cred
$vm = Add-AzVMNetworkInterface -VM $vm -Id $nic.Id
# Create the VMNew-AzVM -VM $vm -ResourceGroupName $resourceGroup -Location $location# Verify that the VM was created$vmList = Get-AzVM -ResourceGroupName $resourceGroup
$vmList.Name

That’s it. Your CentOS VM is ready.

ssh kumara@x.x.x.x

--

--

No responses yet