question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using 'copy' in nested template variables triggered an template validation error

See original GitHub issue

Currently using azurerm-vscode-tools-0.10.1-alpha.20200520.vsix

Following template validation error is triggered: Template validation failed: Internal error: Validation threw an exception: The template function ‘copyIndex’ is not expected at this location. The function can only be used in a resource with copy specified. Please see https://aka.ms/arm-copy for usage details.

ARM template below can be successfully deployed without error:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "location": {
            "type": "string",
            "defaultValue": "[resourceGroup().location]",
            "metadata": {
                "description": "The location where the resources will be deployed."
            }
        },
        "vaultName": {
            "type": "string",
            "metadata": {
                "description": "The name of the keyvault that contains the secret."
            }
        },
        "secretName": {
            "type": "string",
            "metadata": {
                "description": "The name of the secret in key vault, this secret will be admin user password."
            }
        },
        "vaultResourceGroupName": {
            "type": "string",
            "defaultValue": "[resourceGroup().name]",
            "metadata": {
                "description": "The name of the resource group that contains the keyvault."
            }
        },
        "vaultSubscription": {
            "type": "string",
            "defaultValue": "[subscription().subscriptionId]",
            "metadata": {
                "description": "The name of the subscription that contains the keyvault."
            }
        },
        "adminUsername": {
            "type": "string",
            "metadata": {
                "description": "admin user name"
            }
        },
        "virtualNetworksName": {
            "type": "string",
            "metadata": {
                "description": "virtual network name"
            }
        },
        "subnetName": {
            "type": "string",
            "metadata": {
                "description": "subnet name"
            }
        },
        "storageAccountsName": {
            "type": "string",
            "metadata": {
                "description": "Provide storage account name for diagnostic logging"
            }
        },
        "galleriesExternalId": {
            "type": "string",
            "metadata": {
                "description": "Provide resource id of shared image gallery"
            }
        },
        "galleryImageDefinitionName": {
            "type": "string",
            "metadata": {
                "description": "Provide image defination name in shared image gallery"
            }
        },
        "virtualMachinesName": {
            "type": "string",
            "metadata": {
                "description": "Provide virtual machine name"
            }
        },
        "virtualMachinesSize": {
            "type": "string",
            "defaultValue": "Standard_B2s",
            "metadata": {
                "description": "Provide virtual machine size"
            }
        },
        "vmCount": {
            "type": "int",
            "defaultValue": 1,
            "metadata": {
                "description": "Number of VMs to be deployed"
            }
        },
        "dataDiskArray": {
            "metadata": {
                "diskSizeGB": "Disk size in GB",
                "storageAccountType": "Possible values include: 'Standard_LRS', 'Premium_LRS', 'StandardSSD_LRS', 'UltraSSD_LRS'",
                "caching": "Possible values include: 'ReadWrite','ReadOnly','None' By default, disk caching is 'ReadWrite' for the OS disk and 'None' for data disks. ",
                "exampleArray": [
                    {
                        "diskSizeGB": 4,
                        "storageAccountType": "Standard_LRS",
                        "caching": "None"
                    },
                    {
                        "diskSizeGB": 8,
                        "storageAccountType": "Premium_LRS",
                        "caching": "ReadWrite"
                    }
                ]
            },
            "type": "array",
            "defaultValue": [
            ]
        }
    },
    "variables": {
    },
    "resources": [
        {
            "name": "[if(greater(parameters('vmCount'),1),concat(parameters('virtualMachinesName'),copyIndex('vmCopy',1)),parameters('virtualMachinesName'))]",
            "copy": {
                "name": "vmCopy",
                "count": "[parameters('vmCount')]"
            },
            "apiVersion": "2019-10-01",
            "type": "Microsoft.Resources/deployments",
            "properties": {
                "expressionEvaluationOptions": {
                    "scope": "inner"
                },
                "mode": "Incremental",
                "parameters": {
                    "location": {
                        "value": "[parameters('location')]"
                    },
                    "adminUsername": {
                        "value": "[parameters('adminUsername')]"
                    },
                    "adminPassword": {
                        "reference": {
                            "keyVault": {
                                "id": "[resourceId(parameters('vaultSubscription'), parameters('vaultResourceGroupName'), 'Microsoft.KeyVault/vaults', parameters('vaultName'))]"
                            },
                            "secretName": "[parameters('secretName')]"
                        }
                    },
                    "virtualNetworksName": {
                        "value": "[parameters('virtualNetworksName')]"
                    },
                    "subnetName": {
                        "value": "[parameters('subnetName')]"
                    },
                    "storageAccountsName": {
                        "value": "[parameters('storageAccountsName')]"
                    },
                    "galleriesExternalId": {
                        "value": "[parameters('galleriesExternalId')]"
                    },
                    "galleryImageDefinitionName": {
                        "value": "[parameters('galleryImageDefinitionName')]"
                    },
                    "virtualMachinesName": {
                        "value": "[if(greater(parameters('vmCount'),1),concat(parameters('virtualMachinesName'),copyIndex('vmCopy',1)),parameters('virtualMachinesName'))]"
                    },
                    "virtualMachinesSize": {
                        "value": "[parameters('virtualMachinesSize')]"
                    },
                    "dataDiskArray": {
                        "value": "[parameters('dataDiskArray')]"
                    }
                },
                "template": {
                    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                    "contentVersion": "1.0.0.0",
                    "parameters": {
                        "location": {
                            "type": "string"
                        },
                        "virtualNetworksName": {
                            "type": "String"
                        },
                        "subnetName": {
                            "type": "string"
                        },
                        "storageAccountsName": {
                            "type": "String"
                        },
                        "galleriesExternalId": {
                            "type": "String"
                        },
                        "galleryImageDefinitionName": {
                            "type": "string"
                        },
                        "adminUsername": {
                            "type": "string"
                        },
                        "adminPassword": {
                            "type": "securestring"
                        },
                        "virtualMachinesName": {
                            "type": "String"
                        },
                        "virtualMachinesSize": {
                            "type": "string"
                        },
                        "dataDiskArray": {
                            "type": "array"
                        }

                    },
                    "variables": {
                        "subnetId": "[resourceId('Microsoft.Network/virtualNetworks/subnets', parameters('virtualNetworksName'), parameters('subnetName'))]",
                        "imageId": "[concat(parameters('galleriesExternalId'), '/images/',parameters('galleryImageDefinitionName'))]",
                        "copy": [
                            {
                                "name": "dataDisks",
                                "count": "[if(equals(length(parameters('dataDiskArray')),0),1, length(parameters('dataDiskArray')))]",
                                "input": {
                                    "name": "[concat(parameters('virtualMachinesName'),'_DataDisk',copyIndex('dataDisks',1))]",
                                    "lun": "[if(equals(length(parameters('dataDiskArray')),0),0, copyIndex('dataDisks'))]",
                                    "createOption": "Empty",
                                    "diskSizeGB": "[if(equals(length(parameters('dataDiskArray')),0),4,parameters('dataDiskArray')[copyIndex('dataDisks')].diskSizeGB)]",
                                    "caching": "[if(equals(length(parameters('dataDiskArray')),0),'none',parameters('dataDiskArray')[copyIndex('dataDisks')].caching)]",
                                    "managedDisk": {
                                        "storageAccountType": "[if(equals(length(parameters('dataDiskArray')),0),'Standard_LRS',parameters('dataDiskArray')[copyIndex('dataDisks')].storageAccountType)]"
                                    }
                                }
                            }
                        ]
                    },
                    "resources": [
                        // Linux server and related resources
                        {
                            "type": "Microsoft.Network/networkInterfaces",
                            "apiVersion": "2020-03-01",
                            "name": "[concat(parameters('virtualMachinesName'),'_NIC1')]",
                            "location": "[parameters('location')]",
                            "properties": {
                                "ipConfigurations": [
                                    {
                                        "name": "ipconfig1",
                                        "properties": {
                                            "privateIPAllocationMethod": "Dynamic",
                                            "subnet": {
                                                "id": "[variables('subnetId')]"
                                            },
                                            "primary": true,
                                            "privateIPAddressVersion": "IPv4"
                                        }
                                    }
                                ],
                                "dnsSettings": {
                                    "dnsServers": [
                                    ]
                                },
                                "enableAcceleratedNetworking": false,
                                "enableIPForwarding": false
                            }
                        },
                        {
                            "type": "Microsoft.Compute/virtualMachines",
                            "apiVersion": "2019-07-01",
                            "name": "[parameters('virtualMachinesName')]",
                            "location": "[parameters('location')]",
                            "dependsOn": [
                                "[resourceId('Microsoft.Network/networkInterfaces', concat(parameters('virtualMachinesName'),'_NIC1'))]"
                            ],
                            "properties": {
                                "hardwareProfile": {
                                    "vmSize": "[parameters('virtualMachinesSize')]"
                                },
                                "storageProfile": {
                                    "imageReference": {
                                        "id": "[variables('imageId')]"
                                    },
                                    "osDisk": {
                                        "osType": "Linux",
                                        "name": "[concat(parameters('virtualMachinesName'), '_OsDisk')]",
                                        "createOption": "FromImage",
                                        "caching": "ReadWrite",
                                        "managedDisk": {
                                            "StorageAccountType": "Standard_LRS"
                                        }
                                    },
                                    "dataDisks": "[if(equals(length(parameters('dataDiskArray')),0),json('null'),variables('dataDisks'))]"
                                },
                                "osProfile": {
                                    "computerName": "[parameters('virtualMachinesName')]",
                                    "adminUsername": "[parameters('adminUsername')]",
                                    "adminPassword": "[parameters('adminPassword')]",
                                    "linuxConfiguration": {
                                        "disablePasswordAuthentication": false,
                                        "provisionVMAgent": true
                                    },
                                    "secrets": [
                                    ]
                                },
                                "networkProfile": {
                                    "networkInterfaces": [
                                        {
                                            "id": "[resourceId('Microsoft.Network/networkInterfaces', concat(parameters('virtualMachinesName'),'_NIC1'))]"
                                        }
                                    ]
                                },
                                "diagnosticsProfile": {
                                    "bootDiagnostics": {
                                        "enabled": true,
                                        "storageUri": "[concat('https://', parameters('storageAccountsName'), '.blob.core.windows.net/')]"
                                    }
                                }
                            }
                        }
                    ]
                }

            }
        }
    ],
    "outputs": {
    }
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
StephenWeatherfordcommented, Jun 16, 2020

@mvaferreira There is no validation cache, at the moment I think any copyIndex inside a nested template causes problems. @jorgecotillo is working on that.

@jorgecotillo It’s also odd that there’s no error location.

0reactions
jorgecotillocommented, Jul 10, 2020

Thanks @StephenWeatherford I was able to repro the issue and was able to fix it, it was related to the expansion of copy function. The fix will be in v1.0.20

Read more comments on GitHub >

github_iconTop Results From Across the Web

Quickstart: Troubleshoot ARM template JSON deployments
Create a template with errors. Copy the following template and save it locally. You'll use this file to troubleshoot a validation error, a ......
Read more >
ARM template: Way to workaround for nested copy in variable ...
Regarding the firstone I am getting below error: New-AzResourceGroupDeployment : 11:21:44 - Error: Code=InvalidTemplate; Message=Deployment  ...
Read more >
How to troubleshoot Azure Resource Manager (ARM ...
A template validation error means there is a problem with the syntax or layout of your deployment template– this prevents the deployment from ......
Read more >
template Stanza - Job Specification | Nomad
The "template" block instantiates an instance of a template renderer. This creates a convenient way to ship configuration files that are populated from ......
Read more >
Where variables can be used - GitLab Docs
The expanded part needs to be in a form of $variable , or ${variable} or %variable% . ... Pipeline trigger jobs cannot use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found