Search code examples
azurepowershellazure-rm-template

Azure Availability Zone ARM Config


I'm trying to create an ARM template which deploys a VM to a specified zone, however I keep getting the following error. Not sure why, I've checked online and followed the other examples, but still stuck.

The code for the zone template

   "zone": {
      "type": "string",
      "defaultValue": "1",
      "allowedValues": [
        "1",
        "2",
        "3"
      ],
      "metadata": {
        "description": "Zone number for the virtual machine"
      }
    },

The Parameter file is:

"zone": {
  "value": "2"
},

But the error I get is:

New-AzResourceGroupDeployment : 10:11:08 - Error: Code=InvalidTemplate; Message=Deployment template parse failed: 'Error converting value "2" to type 'System.String[]'. 
Path ''.'.
At line:7 char:1

Can anyone point me in the right direction. I have tried using the zone as an array and integer, but neither seem to work.

Thanks in advance :)

############ Edit Parameter file

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "vmName": {
      "value": "vm2002"
    },
    "adminUsername": {
      "value": "localadmin"
    },
    "adminPasswordOrKey": {
      "value": "P@ssword1234"
    },
    "ubuntuOSVersion": {
      "value": "18.04-LTS"
    },
    "VmSize": {
      "value": "Standard_d2-v4"
    },
    "networkName": {
      "value": "tt-vnet"
    },
    "networkResourceGroup": {
      "value": "deployrg3"
    },
    "subnetName": {
      "value": "tt-sub1"
    },
    "zone": {
      "value": [
        "2"
      ]
    },
    "diskCount": {
      "value": 2
    },
    "diskSize": {
      "value": [
        "32"
      ]
    }
  }
}

Template File

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "vmName": {
      "type": "string",
      "defaultValue": "linuxvm",
      "metadata": {
        "description": "The name of you Virtual Machine."
      }
    },
    "adminUsername": {
      "type": "string",
      "metadata": {
        "description": "Username for the Virtual Machine."
      }
    },
    "authenticationType": {
      "type": "string",
      "defaultValue": "password",
      "allowedValues": [
        "sshPublicKey",
        "password"
      ],
      "metadata": {
        "description": "Type of authentication to use on the Virtual Machine. SSH key is recommended."
      }
    },
    "adminPasswordOrKey": {
      "type": "securestring",
      "metadata": {
        "description": "SSH Key or password for the Virtual Machine. SSH key is recommended."
      }
    },
    "ubuntuOSVersion": {
      "type": "string",
      "defaultValue": "18.04-LTS",
      "allowedValues": [
        "12.04.5-LTS",
        "14.04.5-LTS",
        "16.04.0-LTS",
        "18.04-LTS"
      ],
      "metadata": {
        "description": "The Ubuntu version for the VM. This will pick a fully patched image of this given Ubuntu version."
      }
    },
    "VmSize": {
      "type": "string",
      "defaultValue": "Standard_B2s",
      "metadata": {
        "description": "The size of the VM"
      }
    },
    "networkName": {
      "type": "string",
      "minLength": 1
    },
    "networkResourceGroup": {
      "type": "string",
      "minLength": 1
    },
    "subnetName": {
      "type": "string",
      "minLength": 1
    },
    "zone": {
      "type": "array"
      "defaultValue": "1",
      "allowedValues": [
        "1",
        "2",
        "3"
      ],
      "metadata": {
        "description": "Zone number for the virtual machine"
      }
    },
    "diskCount": {
      "type": "int",
      "defaultValue": 1
    },
    "diskSize": {
      "type": "array"
    }
  },
  "variables": {
    "cnt": "[Parameters('diskCount')]",
    "osDiskType": "Premium_LRS",
    "VNetID": "[resourceId(Parameters('networkResourceGroup'), 'Microsoft.Network/virtualNetworks', Parameters('networkname'))]",
    "SubnetRef": "[concat(variables('VNetID'), '/subnets/', Parameters('subnetName'))]",
    "networkInterfaceName": "[concat(Parameters('VMName'), '-nic1')]",
    "linuxConfiguration": {
      "disablePasswordAuthentication": true,
      "ssh": {
        "publicKeys": [
          {
            "path": "[concat('/home/', parameters('adminUsername'), '/.ssh/authorized_keys')]",
            "keyData": "[parameters('adminPasswordOrKey')]"
          }
        ]
      }
    }
  },
  "resources": [
    {
      "type": "Microsoft.Network/networkInterfaces",
      "apiVersion": "2020-05-01",
      "name": "[variables('networkInterfaceName')]",
      "location": "[resourceGroup().location]",
      "properties": {
        "ipConfigurations": [
          {
            "name": "ipconfig1",
            "properties": {
              "subnet": {
                "id": "[variables('subnetRef')]"
              },
              "privateIPAllocationMethod": "Dynamic"
            }
          }
        ]
      }
    },
    {
      "type": "Microsoft.Compute/virtualMachines",
      "apiVersion": "2020-06-01",
      "name": "[parameters('vmName')]",
      "location": "[resourceGroup().location]",
      "dependsOn": [
        "[variables('networkInterfaceName')]"
      ],
      "zones": "[parameters('zone')]",
      "properties": {
        "hardwareProfile": {
          "vmSize": "[parameters('VmSize')]"
        },
        "storageProfile": {
          "osDisk": {
            "createOption": "fromImage",
            "managedDisk": {
              "storageAccountType": "[variables('osDiskType')]"
            },
            "copy": [
              {
                "name": "datadisks",
                "count": "[variables('cnt')]",
                "input": {
                  "name": "[concat(Parameters('VMName'), '-dataDisk', add(copyIndex('datadisks'), 1))]",
                  "createOption": "Empty",
                  "diskSizeGB": "[Parameters('diskSize')[copyIndex('datadisks')]]",
                  "caching": "None",
                  "lun": "[copyIndex('datadisks')]"
                }
              }
            ]
          },
          "imageReference": {
            "publisher": "Canonical",
            "offer": "UbuntuServer",
            "sku": "[parameters('ubuntuOSVersion')]",
            "version": "latest"
          }
        },
        "networkProfile": {
          "networkInterfaces": [
            {
              "id": "[resourceId('Microsoft.Network/networkInterfaces', variables('networkInterfaceName'))]"
            }
          ]
        },
        "osProfile": {
          "computerName": "[parameters('vmName')]",
          "adminUsername": "[parameters('adminUsername')]",
          "adminPassword": "[parameters('adminPasswordOrKey')]",
          "linuxConfiguration": "[if(equals(parameters('authenticationType'), 'password'), json('null'), variables('linuxConfiguration'))]"
        }
      }
    }
  ],
  "outputs": {
    "adminUsername": {
      "type": "string",
      "value": "[parameters('adminUsername')]"
    }
  }
}

Solution

  • The error message The language expression property array index '1' is out of bounds. is a bit misleading. After my validation. The main problem is using "copy" in dataDisks property there.

    When you define the parameter diskCount number as 2, you should also define 2 matching elements in the parameter diskSize array. Also, I change to define the paramater zone type to string type.

    Here is the working sample:

    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
        "vmName": {
          "type": "string",
          "defaultValue": "linuxvm",
          "metadata": {
            "description": "The name of you Virtual Machine."
          }
        },
        "adminUsername": {
          "type": "string",
          "metadata": {
            "description": "Username for the Virtual Machine."
          }
        },
        "authenticationType": {
          "type": "string",
          "defaultValue": "password",
          "allowedValues": [
            "sshPublicKey",
            "password"
          ],
          "metadata": {
            "description": "Type of authentication to use on the Virtual Machine. SSH key is recommended."
          }
        },
        "adminPasswordOrKey": {
          "type": "securestring",
          "metadata": {
            "description": "SSH Key or password for the Virtual Machine. SSH key is recommended."
          }
        },
        "ubuntuOSVersion": {
          "type": "string",
          "defaultValue": "18.04-LTS",
          "allowedValues": [
            "12.04.5-LTS",
            "14.04.5-LTS",
            "16.04.0-LTS",
            "18.04-LTS"
          ],
          "metadata": {
            "description": "The Ubuntu version for the VM. This will pick a fully patched image of this given Ubuntu version."
          }
        },
        "VmSize": {
          "type": "string",
          "defaultValue": "Standard_B2s",
          "metadata": {
            "description": "The size of the VM"
          }
        },
        "networkName": {
          "type": "string",
          "minLength": 1
        },
        "networkResourceGroup": {
          "type": "string",
          "minLength": 1
        },
        "subnetName": {
          "type": "string",
          "minLength": 1
        },
        "zone": {
          "type": "string",
          "defaultValue": "1",
          "allowedValues": [
            "1",
            "2",
            "3"
          ],
          "metadata": {
            "description": "Zone number for the virtual machine"
          }
        }
        ,
        "diskCount": {
          "type": "int",
          "defaultValue": 1
        },
        "diskSize": {
          "type": "array"
        }
      },
      "variables": {
        "cnt": "[Parameters('diskCount')]",
        "osDiskType": "Premium_LRS",
        "VNetID": "[resourceId(Parameters('networkResourceGroup'), 'Microsoft.Network/virtualNetworks', Parameters('networkname'))]",
        "SubnetRef": "[concat(variables('VNetID'), '/subnets/', Parameters('subnetName'))]",
        "networkInterfaceName": "[concat(Parameters('VMName'), '-nic1')]",
        "linuxConfiguration": {
          "disablePasswordAuthentication": true,
          "ssh": {
            "publicKeys": [
              {
                "path": "[concat('/home/', parameters('adminUsername'), '/.ssh/authorized_keys')]",
                "keyData": "[parameters('adminPasswordOrKey')]"
              }
            ]
          }
        }
      },
      "resources": [
        {
          "type": "Microsoft.Network/networkInterfaces",
          "apiVersion": "2020-05-01",
          "name": "[variables('networkInterfaceName')]",
          "location": "[resourceGroup().location]",
          "properties": {
            "ipConfigurations": [
              {
                "name": "ipconfig1",
                "properties": {
                  "subnet": {
                    "id": "[variables('subnetRef')]"
                  },
                  "privateIPAllocationMethod": "Dynamic"
                }
              }
            ]
          }
        },
        {
          "type": "Microsoft.Compute/virtualMachines",
          "apiVersion": "2020-06-01",
          "name": "[parameters('vmName')]",
          "location": "[resourceGroup().location]",
          "dependsOn": [
            "[variables('networkInterfaceName')]"
          ],
          "zones": [
              "[parameters('zone')]"
              ],
          "properties": {
            "hardwareProfile": {
              "vmSize": "[parameters('VmSize')]"
            },
            "storageProfile": {
    
              "imageReference": {
                "publisher": "Canonical",
                "offer": "UbuntuServer",
                "sku": "[parameters('ubuntuOSVersion')]",
                "version": "latest"
              },
              "osDisk": {
                "createOption": "fromImage",
                "managedDisk": {
                  "storageAccountType": "[variables('osDiskType')]"
                }
              },
    
                "copy": [
    
                    {
                    "name": "dataDisks",
                    "count": "[variables('cnt')]",
                    "input": {
                      "name": "[concat(Parameters('VMName'), '-dataDisk', add(copyIndex('dataDisks'), 1))]",
                      "createOption": "Empty",
                      "diskSizeGB": "[Parameters('diskSize')[copyIndex('dataDisks')]]",
                      "caching": "None",
                      "lun": "[copyIndex('dataDisks')]"
                    }
                  }
    
                ]
    
            },
            "networkProfile": {
              "networkInterfaces": [
                {
                  "id": "[resourceId('Microsoft.Network/networkInterfaces', variables('networkInterfaceName'))]"
                }
              ]
            },
            "osProfile": {
              "computerName": "[parameters('vmName')]",
              "adminUsername": "[parameters('adminUsername')]",
              "adminPassword": "[parameters('adminPasswordOrKey')]",
              "linuxConfiguration": "[if(equals(parameters('authenticationType'), 'password'), json('null'), variables('linuxConfiguration'))]"
            }
          }
        }
      ],
      "outputs": {
        "adminUsername": {
          "type": "string",
          "value": "[parameters('adminUsername')]"
        }
      }
    }
    

    Parameters:

    {
        "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
        "contentVersion": "1.0.0.0",
        "parameters": {
          "vmName": {
            "value": "vm2002"
          },
          "adminUsername": {
            "value": "localadmin"
          },
          "adminPasswordOrKey": {
            "value": "Password"
          },
          "ubuntuOSVersion": {
            "value": "18.04-LTS"
          },
        //   "VmSize": {
        //     "value": "Standard_B1s"
        //   },
          "networkName": {
            "value": "vvvv"
          },
          "networkResourceGroup": {
            "value": "nancyarm"
          },
          "subnetName": {
            "value": "default"
          },
          "zone": {
            "value": "2"      
          },
          "diskCount": {
            "value": 2
          },
          "diskSize": {
            "value": [32,64]
          }
        }
      }