Share via


Invalid Template Unable to process template language expressions for resource (Creating Multiple VM Using Azure Resource Manager Error)

Question

Sunday, July 9, 2017 11:34 AM

Trying to create multiple instance of VM using ARM Template which I composed it creates the first VM in the array but will not create the other two. Below is the error I get, can someone point me in the right direction on where I am going wrong.

12:20:38 - [ERROR] Microsoft.Compute/virtualMachines 'AZ-Test02' failed with message '{
12:20:38 - [ERROR]   "error": {
12:20:38 - [ERROR]     "code": "InvalidTemplate",
12:20:38 - [ERROR]     "message": "Unable to process template language expressions for resource '/
12:20:38 - [ERROR] subscriptions/xxxxx/resourceGroups/viral_test/pr
12:20:38 - [ERROR] oviders/Microsoft.Compute/virtualMachines/AZ-Test02' at line '146' and column 
12:20:38 - [ERROR] '10'. 'The language expression property array index '1' is out of bounds.'"
12:20:38 - [ERROR]   }
12:20:38 - [ERROR] }'

This is the code:

  

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {

    "StorageType": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_ZRS",
        "Standard_GRS",
        "Standard_RAGRS",
        "Premium_LRS"
      ]
    },

    "StorageName": {
      "type": "string",
      "metadata": {
        "description": "Storage Account Name"
      }
    },

    "VirtualNetworkPrefix": {
      "type": "string",
      "defaultValue": "10.0.0.0/24"
    },

    "VirtualNetworkSubnet1Name": {
      "type": "string",
      "defaultValue": "Subnet-1"
    },
    "VirtualMachineName": {
      "type": "array",
      "defaultValue": [

      ]
    },

    "VirtualMachineAdminUserName": {
      "type": "string",
      "minLength": 1,
      "metadata": {
        "description": "Username for the Virtual Machine"
      }
    },

    "VirtualMachineAdminPassword": {
      "type": "securestring",
      "defaultValue": "Azure_Admin",
      "metadata": {
        "description": "Local Password For Virtual Machine"
      }

    },
    "VirtualMachineWindowsOSVersion": {
      "type": "string",
      "defaultValue": "2012-R2-Datacenter",
      "allowedValues": [
        "2008-R2-SP1",
        "2012-Datacenter",
        "2012-R2-Datacenter",
        "2016-Datacenter",
        "Windows-Server-Technical-Preview"
      ]
    },

    "VirtualMachineVmSize": {
      "type": "array",
      "defaultValue": [

      ]
    }

  },
  "variables": {
    "VirtualNetworkPrefix": "[parameters('VirtualNetworkPrefix')]",
    "VirtualNetworkSubnet1Name": "[parameters('VirtualNetworkSubnet1Name')]",
    "VirtualMachineImagePublisher": "MicrosoftWindowsServer",
    "VirtualMachineImageOffer": "WindowsServer",
    "VirtualMachineOSDiskName": "[parameters('VirtualMachineName')]",
    "vmSize": "Standard_A2",
    "VirtualMachineVnetID": "[resourceId('Microsoft.Network/virtualNetworks', 'VirtualNetwork')]",
    "VirtualMachineSubnetRef": "[concat(variables('VirtualMachineVnetID'), '/subnets/', variables('VirtualNetworkSubnet1Name'))]",
    "VirtualMachineStorageAccountContainerName": "vhds"
    },
  "resources": [
    {
      "name": "[parameters('StorageName')]",
      "type": "Microsoft.Storage/storageAccounts",
      "location": "[resourceGroup().location]",
      "apiVersion": "2016-01-01",
      "sku": {
        "name": "[parameters('StorageType')]"
      },
      "dependsOn": [],
      "tags": {
        "displayName": "TestStorage"
      },
      "kind": "Storage"
    },
    {
      "name": "VirtualNetwork",
      "type": "Microsoft.Network/virtualNetworks",
      "location": "[resourceGroup().location]",
      "apiVersion": "2016-03-30",
      "dependsOn": [],
      "tags": {
        "displayName": "VirtualNetwork"
      },
      "properties": {
        "addressSpace": {
          "addressPrefixes": [
            "[variables('VirtualNetworkPrefix')]"
          ]
        },
        "subnets": [
          {
            "name": "[variables('VirtualNetworkSubnet1Name')]",
            "properties": {
              "addressPrefix": "[variables('VirtualNetworkPrefix')]"
            }
          }
        ]
      }
    },
    {
      "name": "[parameters('VirtualMachineName')[copyIndex()]]",
      "type": "Microsoft.Network/networkInterfaces",
      "location": "[resourceGroup().location]",
      "apiVersion": "2016-03-30",
      "dependsOn": [
        "[resourceId('Microsoft.Network/virtualNetworks', 'VirtualNetwork')]"
      ],
      "tags": {
        "displayName": "VirtualMachineNic"
      },
      "properties": {
        "ipConfigurations": [
          {
            "name": "ipconfig1",
            "properties": {
              "privateIPAllocationMethod": "Dynamic",
              "subnet": {
                "id": "[variables('VirtualMachineSubnetRef')]"
              }
            }
          }
        ]
      },
      "copy": {
        "name": "VirtualMachineNic",
        "count": "[length(parameters('VirtualMachineName'))]"
      }
    },
    {
      "name": "[parameters('VirtualMachineName')[copyIndex()]]",
      "type": "Microsoft.Compute/virtualMachines",
      "location": "[resourceGroup().location]",
      "apiVersion": "2015-06-15",
      "dependsOn": [
        "[resourceId('Microsoft.Storage/storageAccounts', parameters('StorageName'))]",
        "[resourceId('Microsoft.Network/networkInterfaces', parameters('VirtualMachineName')[copyIndex()])]"
      ],
      "tags": {
        "displayName": "VirtualMachine"
      },
      "properties": {
        "hardwareProfile": {
          "vmSize": "[parameters('VirtualMachineVmSize')[copyIndex()]]"
        },
        "osProfile": {
          "computerName": "[parameters('VirtualMachineName')[copyIndex()]]",
          "adminUsername": "[parameters('VirtualMachineAdminUsername')]",
          "adminPassword": "[parameters('VirtualMachineAdminPassword')]"
        },
        "storageProfile": {
          "imageReference": {
            "publisher": "[variables('VirtualMachineImagePublisher')]",
            "offer": "[variables('VirtualMachineImageOffer')]",
            "sku": "[parameters('VirtualMachineWindowsOSVersion')]",
            "version": "latest"
          },
          "osDisk": {
            "name": "VirtualMachineOSDisk",
            "vhd": {
              "uri": "[concat(reference(resourceId('Microsoft.Storage/storageAccounts', parameters('StorageName')), '2016-01-01').primaryEndpoints.blob, variables('VirtualMachineStorageAccountContainerName'), '/', variables('VirtualMachineOSDiskName')[copyIndex()], '.vhd')]"
            },
            "caching": "ReadWrite",
            "createOption": "FromImage"
          }
        },
        "networkProfile": {
          "networkInterfaces": [
            {
              "id": "[resourceId('Microsoft.Network/networkInterfaces', parameters('VirtualMachineName')[copyIndex()])]"
            }
          ]
        }
      },
      "copy":
      {
        "name": "VirtualMachine",
        "count": "[length(parameters('VirtualMachineName'))]"
      }

    }

  ],
  "outputs": {}
}

Any help on this would be greatly appreciated. 

All replies (5)

Tuesday, July 11, 2017 4:09 AM ✅Answered

Ensure you have the same number of elements in the VirtualMachineName parameter array and the VirtualMachineVmSize parameter array such as below, otherwise that array index error will come up:

 

    "VirtualMachineName": {

      "type": "array",

      "defaultValue": [

          "vm1",

          "vm2",

          "vm3"

      ]

    },

 

 

    "VirtualMachineVmSize": {

      "type": "array",

      "defaultValue": [

        "Standard_DS2_v2",

        "Standard_DS2_v2",

        "Standard_DS2_v2"       

      ]

    }

Do click on "Mark as Answer" on the post that helps you, this can be beneficial to other community members


Monday, July 10, 2017 5:54 AM

You are re-using the same attribute twice( VirtualNetworkSubnet1Name)  once in parameters, once in variables.

Line 30-33 (parameters)

"VirtualNetworkSubnet1Name": {

      "type": "string",

      "defaultValue": "Subnet-1"

    },

 

 

Line 79 (variables)

VirtualNetworkSubnet1Name": "[parameters('VirtualNetworkSubnet1Name')]",

 

 

You can change the block at line 146 to match”

            "name": "ipconfig1",

            "properties": {

              "privateIPAllocationMethod": "Dynamic",

              "subnet": {

                "id": "[concat(resourceId('Microsoft.Network/virtualNetworks', parameters('VnetName')), '/subnets/', parameters('SubnetName'))]"

 

And remove line 79 completely.

 

Which saves an additional step of defining the subnet in a variable.  

 

Do click on "Mark as Answer" on the post that helps you, this can be beneficial to other community members


Monday, July 10, 2017 8:39 PM

Still having issue. Tweaked the template to the below:

Error cannot create the other two VM's in the array

"error": { "code": "InvalidTemplate", "message": "Unable to process template language expressions for resource '/subscriptions/xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx/resourceGroups/xxxx_test/providers/Microsoft.Compute/virtualMachines/AZ-Test03' at line '150' and column '10'. 'The language expression property array index '2' is out of bounds.'" } }

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {

    "StorageType": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_ZRS",
        "Standard_GRS",
        "Standard_RAGRS",
        "Premium_LRS"
      ]
    },

    "VnetName": {
      "type": "string",
      "defaultValue": "VirtualNetwork",
      "metadata": {
        "description": "VirtualNetwork Name"
      }
    },

    "StorageName": {
      "type": "string",
      "metadata": {
        "description": "Storage Account Name"
      }
    },

    "VirtualNetworkPrefix": {
      "type": "string",
      "defaultValue": "10.0.0.0/24"
    },

    "VirtualNetworkSubnet1Name": {
      "type": "string",
      "defaultValue": "Subnet-1"
    },
    "VirtualMachineName": {
      "type": "array",
      "defaultValue": [

      ]
    },

    "VirtualMachineAdminUserName": {
      "type": "string",
      "minLength": 1,
      "metadata": {
        "description": "Username for the Virtual Machine"
      }
    },

    "VirtualMachineAdminPassword": {
      "type": "securestring",
      "defaultValue": "Azure_Admin",
      "metadata": {
        "description": "Local Password For Virtual Machine"
      }

    },
    "VirtualMachineWindowsOSVersion": {
      "type": "string",
      "defaultValue": "2012-R2-Datacenter",
      "allowedValues": [
        "2008-R2-SP1",
        "2012-Datacenter",
        "2012-R2-Datacenter",
        "2016-Datacenter",
        "Windows-Server-Technical-Preview"
      ]
    },

    "VirtualMachineVmSize": {
      "type": "array",
      "defaultValue": [

      ]
    }

  },
  "variables": {
    "VirtualNetworkPrefix": "[parameters('VirtualNetworkPrefix')]",
    "VirtualMachineImagePublisher": "MicrosoftWindowsServer",
    "VirtualMachineImageOffer": "WindowsServer",
    "VirtualMachineStorageAccountContainerName": "vhds",
    "Subnet": "/subscriptions/xxxxxx/resourceGroups/viral_test/providers/Microsoft.Network/virtualNetworks/VirtualNetwork/subnets/subnet-1"
  },
  "resources": [
    {
      "name": "[parameters('StorageName')]",
      "type": "Microsoft.Storage/storageAccounts",
      "location": "[resourceGroup().location]",
      "apiVersion": "2016-01-01",
      "sku": {
        "name": "[parameters('StorageType')]"
      },
      "dependsOn": [],
      "tags": {
        "displayName": "TestStorage"
      },
      "kind": "Storage"
    },
    {
      "name": "VirtualNetwork",
      "type": "Microsoft.Network/virtualNetworks",
      "location": "[resourceGroup().location]",
      "apiVersion": "2016-03-30",
      "dependsOn": [],
      "tags": {
        "displayName": "VirtualNetwork"
      },
      "properties": {
        "addressSpace": {
          "addressPrefixes": [
            "[variables('VirtualNetworkPrefix')]"
          ]
        },
        "subnets": [
          {
            "name": "[parameters('VirtualNetworkSubnet1Name')]",
            "properties": {
              "addressPrefix": "[variables('VirtualNetworkPrefix')]"
            }
          }
        ]
      }
    },
    {
      "name": "[parameters('VirtualMachineName')[copyIndex()]]",
      "type": "Microsoft.Network/networkInterfaces",
      "location": "[resourceGroup().location]",
      "apiVersion": "2016-03-30",
      "dependsOn": [
        "[resourceId('Microsoft.Network/virtualNetworks', 'VirtualNetwork')]"
      ],
      "tags": {
        "displayName": "VirtualMachineNic"
      },
      "properties": {
        "ipConfigurations": [
          {
            "name": "ipconfig1",
            "properties": {
              "privateIPAllocationMethod": "Dynamic",
              "subnet": {
                "id": "[variables('Subnet')]"
              }
            }
          }
        ]
      },
      "copy": {
        "name": "VirtualMachineNic",
        "count": "[length(parameters('VirtualMachineName'))]"
      }
    },
    {
      "name": "[parameters('VirtualMachineName')[copyIndex()]]",
      "type": "Microsoft.Compute/virtualMachines",
      "location": "[resourceGroup().location]",
      "apiVersion": "2015-06-15",
      "dependsOn": [
        "[resourceId('Microsoft.Storage/storageAccounts', parameters('StorageName'))]",
        "[resourceId('Microsoft.Network/networkInterfaces', parameters('VirtualMachineName')[copyIndex()])]"
      ],
      "tags": {
        "displayName": "VirtualMachine"
      },
      "properties": {
        "hardwareProfile": {
          "vmSize": "[parameters('VirtualMachineVmSize')[copyIndex()]]"
        },
        "osProfile": {
          "computerName": "[parameters('VirtualMachineName')[copyIndex()]]",
          "adminUsername": "[parameters('VirtualMachineAdminUsername')]",
          "adminPassword": "[parameters('VirtualMachineAdminPassword')]"
        },
        "storageProfile": {
          "imageReference": {
            "publisher": "[variables('VirtualMachineImagePublisher')]",
            "offer": "[variables('VirtualMachineImageOffer')]",
            "sku": "[parameters('VirtualMachineWindowsOSVersion')]",
            "version": "latest"
          },
          "osDisk": {
            "name": "VirtualMachineOSDisk",
            "vhd": {
              "uri": "[concat(reference(resourceId('Microsoft.Storage/storageAccounts', parameters('StorageName')), '2016-01-01').primaryEndpoints.blob, variables('VirtualMachineStorageAccountContainerName'), '/', parameters('VirtualMachineName')[copyIndex()], '.vhd')]"
            },
            "caching": "ReadWrite",
            "createOption": "FromImage"
          }
        },
        "networkProfile": {
          "networkInterfaces": [
            {
              "id": "[resourceId('Microsoft.Network/networkInterfaces', parameters('VirtualMachineName')[copyIndex()])]"
            }
          ]
        }
      },
      "copy":
      {
        "name": "VirtualMachine",
        "count": "[length(parameters('VirtualMachineName'))]"
      }

    }

  


  ],
  "outputs": {}
}

Tuesday, July 11, 2017 7:52 AM

Hi Monika,

That was the case forgot to add other VM size in the array (D'oh) thank you for your help.


Tuesday, July 18, 2017 9:41 AM

Glad to know that above post helped you.

Do click on "Mark as Answer" on the post that helps you, this can be beneficial to other community members