azureazure-cliazure-availability-set

How to specify availability set in template deployment?


I created a linux image and my intention is to create other VMs from the same VHD. These VMs would need to be in an availability set - so is there a param to specify the availability set name using this command?

azure group deployment create --resource-group myRG --template-file temp.json

Ref: https://azure.microsoft.com/en-gb/documentation/articles/virtual-machines-linux-capture-image/


Solution

  • In your temp.json file add the availabilitySetName as a variable (change accordingly):

    "variables": {
     ...
      "availabilitySetName": "myAvSet",
     ...
     }
    

    Then add it as a resource:

    "resources": [
     ...
     {
      "type": "Microsoft.Compute/availabilitySets",
      "name": "[variables('availabilitySetName')]",
      "apiVersion": "2015-06-15",
      "location": "[resourceGroup().location]",
      "properties": {}
     }
     ...
    

    Down further still inside "resources", find the virtual machine that you want to add to an availabilitySet and make it dependable on your availabilitySet resource. Just after modifying dependsOn, add it to the properties object.

     {
      "apiVersion": "2015-06-15",
      "type": "Microsoft.Compute/virtualMachines",
      ...
      "dependsOn": [
        ...
        "[concat('Microsoft.Compute/availabilitySets/', variables('availabilitySetName'))]"
        ...
      ],
      "properties": {
        ...
        "availabilitySet": {
          "id": "[resourceId('Microsoft.Compute/availabilitySets',variables('availabilitySetName')) ]"
        }
      ...
      }
    

    Update:

    When creating a VM from an image, the easiest way is to just create the availability set before deploying the template just like we already do with the network interface. In this case, you only have to reference the resource in "properties" object.

    "properties": {
        ...
        "availabilitySet": {
          "id": "[resourceId('Microsoft.Compute/availabilitySets', 'myAsName') ]"
        }
      ...