Share via

I'm getting error while deploying DataDisks

Ramakrishna Abhijeet P 70 Reputation points
2025-03-18T12:44:31.7666667+00:00

Below is a Part of ARM template that I'm creating to use as a base template to create Virtual Machines. Here I'm using Azure compute galleries images as image reference with passing as allowed list. I'm getting error as below and disks are getting created but not attaching to virtual machines.

Unable to process template language expressions for resource '/subscriptions/SubscriptionID/resourceGroups/RGName/providers/Microsoft.Compute/virtualMachines/VMName' at line '291' and column '9'. 'The template function 'copyIndex' is not expected at this location. The function can only be used in a resource with copy specified.

        {
            "apiVersion": "[variables('apiVersion')]",
            "type": "Microsoft.Compute/virtualMachines",
            "name": "[variables('vmName')]",
            "location": "[parameters('location')]",
            "zones": "[if(equals(length(parameters('availabilityZone')),0), json('null'), array(parameters('availabilityZone')))]",
            "tags": {
                "description": "[parameters('description')]"
            },
            "properties": {
                "hardwareProfile": {
                    "vmSize": "[parameters('size')]"
                },
                "storageProfile": {
                    "imageReference": "[variables('imageReference')]",
                    "osDisk": {
                        "createOption": "FromImage",
                        "diskSizeGB": "[variables('convertToOsDiskSizeGB')[string(equals(parameters('osDiskSize'), 0))]]",
                        "managedDisk": {
                            "storageAccountType": "[parameters('osDiskType')]"
                        }
                    },
                    "dataDisks": [
                        {
                            "copy": {
                                "count": "[length(parameters('datadisks'))]",
                                "name": "dataDiskCopy"
                            },
                            "lun": "[copyIndex('dataDiskCopy')]",
                            "createOption": "Attach",
                            "managedDisk": {
                                "id": "[resourceId('Microsoft.Compute/disks', concat(variables('vmName'), '-datadisk-', copyIndex('dataDiskCopy')))]"
                            }
                        }
                    ]
                },
                "osProfile": {
                    "computerName": "[variables('vmName')]",
                    "adminUsername": "vm-admin",
                    "adminPassword": "[variables('adminPassword')]"
                },
                "networkProfile": {
                    "networkInterfaces": [
                        {
                            "id": "[resourceId('Microsoft.Network/networkInterfaces', concat(variables('vmName'), '-nic'))]"
                        }
                    ]
                },
                "diagnosticsProfile": {
                    "bootDiagnostics": {
                        "enabled": true
                    }
                },
                "availabilitySet": "[if(empty(parameters('availabilitySet')), json('null'), reference(resourceId('Microsoft.Compute/availabilitySets', parameters('availabilitySet'))))]"
            }
        },
        {
            "condition": "[greater(length(parameters('datadisks')), 0)]",
            "apiVersion": "2019-07-01",
            "type": "Microsoft.Compute/disks",
            "name": "[concat(variables('vmName'), '-datadisk-', copyIndex('diskcopy'))]",
            "location": "[parameters('location')]",
            "copy": {
                "name": "diskcopy",
                "count": "[length(parameters('datadisks'))]"
            },
            "sku": {
                "name": "[parameters('datadisks')[copyIndex('diskcopy')].storageAccountType]"
            },
            "properties": {
                "creationData": {
                    "createOption": "Empty"
                },
                "diskSizeGB": "[parameters('datadisks')[copyIndex('diskcopy')].diskSizeGB]"
            }
        }
Azure Virtual Machines
Azure Virtual Machines

An Azure service that is used to provision Windows and Linux virtual machines.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.