You have 3 azure instances (VMs) that you need to provison in 3 diff availability zones under the same region? how do you automate provisioning this infrastructure using power shell?

Prasad 1 Reputation point

In azure cloud 3 instances like VM's, these 3 VM's should be deployed in 3 diff virtual networks and under 3 availability zones, so we achieve High Availability,in case one of the availability zone goes offline (down) my app still run through other availability zone, all 3 availability zones are under single region say East US so how will you write power shell for this or how will you automate this infrastructure provisioning on azure cloud?

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,545 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. shiva patpi 13,171 Reputation points Microsoft Employee

    Hello @Prasad ,
    Please use the below customized script.
    Below PowerShell script will create 3 Ubuntu VMs in 3 different Availability Zones

    $ResourceGroup = "AvailabilityZonesDemo" #ResourceGroupname (All the VMs will be created under single resource group)
    $Location = "EastUS" #Location where to create the VMs
    $vNetName = "avzone-vnet" # VirtualNetwork name
    $AddressSpace = "" #Address space for VNET
    $SubnetIPRange = "" #Subnet under the VNET
    $SubnetName = "avzone-subnet" #SubnetName
    $nsgName = "avzone-multi" #NSG rules for the VMs

    New-AzureRmResourceGroup -Name $ResourceGroup -Location $Location

    Create Virtual Network and Subnet

    $vNetwork = New-AzureRmVirtualNetwork -ResourceGroupName $ResourceGroup -Name $vNetName -AddressPrefix $AddressSpace -Location $location
    Add-AzureRmVirtualNetworkSubnetConfig -Name $SubnetName -VirtualNetwork $vNetwork -AddressPrefix $SubnetIPRange
    Set-AzureRmVirtualNetwork -VirtualNetwork $vNetwork

    Create Network Security Group

    $nsgRuleVMAccess = New-AzureRmNetworkSecurityRuleConfig -Name 'allow-vm-access' -Protocol Tcp -Direction Inbound -Priority 100 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 22,3389 -Access Allow
    New-AzureRmNetworkSecurityGroup -ResourceGroupName $ResourceGroup -Location $location -Name $nsgName -SecurityRules $nsgRuleVMAccess

    Define Variables needed for Virtual Machine

    $vNet = Get-AzureRmVirtualNetwork -ResourceGroupName $ResourceGroup -Name $vNetName
    $Subnet = Get-AzureRmVirtualNetworkSubnetConfig -Name $SubnetName -VirtualNetwork $vNet
    $nsg = Get-AzureRmNetworkSecurityGroup -ResourceGroupName $ResourceGroup -Name $NsgName
    $vmName = "VMName"
    $pubName = "Canonical"
    $offerName = "UbuntuServer"
    $skuName = "18.04-LTS"
    $vmSize = "Standard_Ds1_v2" #Change the SKU size accordingly
    $pipName = "$vmName-pip"
    $nicName = "$vmName-nic"
    $osDiskName = "$vmName-OsDisk"
    $osDiskSize = "30"
    $osDiskType = "Premium_LRS"

    Create Admin Credentials

    $adminUsername = 'admin'
    $adminPassword = 'Password1234567$'

    [String] [ValidateNotNullOrEmpty()] $secpasswd = "Password1234567"
    [SecureString] $secpasswd = ConvertTo-SecureString -String "Password1234567" -AsPlainText -Force
    [PSCredential] $adminCreds = New-Object System.Management.Automation.PSCredential (“fortuneuser”, $secpasswd)

    //Below code will create 3 Virtual Machines in different Availability zones
    for($i=1;$i -le 3;$i++){

    // Create a public IP and NIC
    $pip = New-AzureRmPublicIpAddress -Name $pipName$i -ResourceGroupName $ResourceGroup -Location $location -AllocationMethod Static -Zone $i -Sku Standard
    $nic = New-AzureRmNetworkInterface -Name $nicName$i -ResourceGroupName $ResourceGroup -Location $location -SubnetId $Subnet.Id -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $nsg.Id

    Set VM Configuration

    $vmConfig = New-AzureRmVMConfig -VMName $vmName$i -VMSize $vmSize -Zone $i
    Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $nic.Id

    Set VM operating system parameters

    Set-AzureRmVMOperatingSystem -VM $vmConfig -Linux -ComputerName $vmName -Credential $adminCreds

    Set boot diagnostic storage account

    Set-AzureRmVMBootDiagnostics -Enable -ResourceGroupName $ResourceGroup -VM $vmConfig #-StorageAccountName $StorageAccount

    Set virtual machine source image

    Set-AzureRmVMSourceImage -VM $vmConfig -PublisherName $pubName -Offer $offerName -Skus $skuName -Version 'latest'

    Set OsDisk configuration

    Set-AzureRmVMOSDisk -VM $vmConfig -Name $osDiskName$i -DiskSizeInGB $osDiskSize -StorageAccountType $osDiskType -CreateOption fromImage

    Create the VM

    New-AzureRmVM -ResourceGroupName $ResourceGroup -Location $location -VM $vmConfig

    You can use the below reference document

    Let me know if you have additional questions.


    1 person found this answer helpful.
    0 comments No comments