Provision Azure VM via PowerShell -ARM Model

In Azure cloud platform, we can find two models for deploying workloads & resources. Azure Service Management model (Classic model) and Azure Resource Manager (ARM) lets get a brief idea about it.

What’s Azure Resource Manager ?

“An Azure Resource Group is a logical container for grouping Azure resources. As an example,consider a typical website implementation that has a web front end with which users interact and a database in which to store data. The web front end and the database are individual resources that comprise the full website solution. An Azure Resource Group gives you a natural way to manage and monitor resources that comprise a solution.”

ASM vs ARM

Azure Service Manager 

  • XML-based REST API
  • Separate Xplat-CLI modes,portals & PowerShell modules
  • Resources are created and managed separately in cloud service
  • Use “Classic portal” for configuration
  • Portals is Silverlight dependent and in future Silverlight will be depreciated

Azure Resource Manager 

  • Use more efficient JSON-based REST API
  • Resources are JSON template-based containers
  • Taxonomic tagging (cost)
  • Download pre built JSON files for workload deployment
  •  Role-based Access Control (RBAC)
  • Portal developed using  HTML 5 web standeds

From this post I’ll walk-through how to provisioned Windows and Linux VM using PowerShell ARM cmdlets

  1. First you should install PowerShell module for azure refer link.
  2. Login to Azure Resource Manager Account.
    login RM account
  3. In this step creating Resource Group and Storage Account for VM.
    $resourceGroup ='armvmdeploy'
    $location = 'Southeast Asia'
    
    #create a resource group
    New-AzureRmResourceGroup -Name $resourceGroup -Location $location
    
    #create a storage account
    $storageAccount = 'kasunstorage100'
    New-AzureRmStorageAccount -Name $storageAccount -ResourceGroupName $resourceGroup -Location $location -Type Standard_GRS
    
  4. Create VNet for VM(s)
    #create Vnet
    $vnetName = 'webappvnet'
    $subnet = New-AzureRmVirtualNetworkSubnetConfig -Name frontendSubnet -AddressPrefix 10.0.1.0/24
    $vnet = New-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $resourceGroup -Location $location -AddressPrefix 10.0.0.0/16 -Subnet $subnet
    
  5. So its time to create a Windows VM before that we have to create a NIC and assign a Public IP to VM.
    #Windows VM
    #create NIC and Public IP
    $nicName = 'vm1-nic'
    $publicIP = New-AzureRmPublicIpAddress -Name $nicName -ResourceGroupName $resourceGroup -Location $location -AllocationMethod Dynamic
    $nic =New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $resourceGroup -Location $location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $publicIP.Id
    
  6. We created the relevant resources to create a VM,so lets create the VM use following cmdlets. You will prompt to enter the username and password for VM.
    #create a VM
    $vmName = 'win-web'
    $vm = New-AzureRmVMConfig -VMName $vmName -VMSize 'Basic_A1'
    
    $cred = Get-Credential -Message 'Enter the Credentials'
    $vm = Set-AzureRmVMOperatingSystem -VM $vm -Windows -ComputerName $vmName -Credential $cred -ProvisionVMAgent -EnableAutoUpdate
    $vm = Set-AzureRmVMSourceImage -VM $vm -PublisherName 'MicrosoftWindowsServer' -Offer 'WindowsServer' -Skus '2012-R2-Datacenter' -Version 'latest'
    
    $vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
    
    $diskName = 'os-disk'
    $storageAcc = Get-AzureRmStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccount
    $osDisk = $storageAcc.PrimaryEndpoints.Blob.ToString() + 'vhds/' + $diskName + '.vhd'
    $vm = Set-AzureRmVMOSDisk -VM $vm -Name $diskName -VhdUri $osDisk -CreateOption FromImage
    
    New-AzureRmVM -ResourceGroupName $resourceGroup -Location $location -VM $vm -Verbose
    
    

We can use the above code to create a Linux VM, following are the full code snippet for it. Used same Resource Group,VNet and Storage Account.

#Linux VM

$nic2Name = 'vm2-nic'
$publicIP2 = New-AzureRmPublicIpAddress -Name $nic2Name -ResourceGroupName $resourceGroup -Location $location -AllocationMethod Dynamic
$nic2 =New-AzureRmNetworkInterface -Name $nic2Name -ResourceGroupName $resourceGroup -Location $location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $publicIP2.Id

$vm2name = 'ubuntu-proxy'
$vm2 = New-AzureRmVMConfig -VMName $vm2name -VMSize 'Basic_A1'

$cred2 = Get-Credential -Message 'Enter the Credentials'
$vm2 = Set-AzureRmVMOperatingSystem -VM $vm2 -Linux -ComputerName $vm2Name -Credential $cred2
$vm2 = Set-AzureRmVMSourceImage -VM $vm2 -PublisherName 'canonical' -Offer 'UbuntuServer' -Skus '14.04.4-LTS' -Version 'latest'
$vm2 = Add-AzureRmVMNetworkInterface -VM $vm2 -Id $nic2.Id

$disk2Name = 'ub-os-disk'
$storageAcc = Get-AzureRmStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccount
$osDisk2 = $storageAcc.PrimaryEndpoints.Blob.ToString() + 'vhds/' + $disk2Name + '.vhd'
$vm2 = Set-AzureRmVMOSDisk -VM $vm2 -Name $disk2Name -VhdUri $osDisk2 -CreateOption FromImage

New-AzureRmVM -ResourceGroupName $resourceGroup -Location $location -VM $vm2 -Verbose

Following is a screen capture of the resource group.
RG

, , , , , , ,

Post navigation