So I have a template that is going to deploy multiple private endpoints in a Cosmosdb. For it I have a Vnet Array with 5 Vnets:
"vnetArray": {
"type": "array",
"defaultValue": [
"Vnet-1ab-1",
"Vnet-2ab-8",
"Vnet-3ab-9",
"Vnet-4ab-34",
"Vnet-5ab"
],
And also inside those vnets 4 subnets. Subnets are named the same in all the vnets.
"subnetArray": {
"type": "array",
"defaultValue": ["Subet-a","Subnet-b","Subnet-c","Subnet-d"],
"metadata": {
}
This will give 20 different private enpoints which will need to have their unique name like: "subnet-a-1ab-1-privateendpoint" So I have this following copy function code that uses the "mul" function to get number of private endpoints doing multiplication between subnets and vnets:
"resources": [
{
"type": "Microsoft.Network/privateEndpoints",
"apiVersion": "[variables('privateEndpointVersion')]",
"copy":{
"name": "peLoop",
"count": "[mul(length(parameters('vnetArray')), length(parameters('subnetArray')))]"
},
"name": "[concat(parameters('subnetArray')[copyIndex('peLoop')], '-mongo-pe-', replace(parameters('vnetArray')[copyIndex('peLoop')], 'Vnet-', ''))]",
"location": "[parameters('regionArray')[copyIndex()]]",
"properties": {
"customNetworkInterfaceName"
The problem is that I´m receiveing this error and not sure what can be related to.
{"code":"InvalidTemplate","message":"Deployment template validation failed: 'The template resource '[concat(parameters('subnetArray')[copyIndex('privateEndpointLoop')], '-mongo-pe-', replace(parameters('vnetArray')[copyIndex('peLoop')], 'Vnet-', ''))]' at line '97' and column '9' is not valid: The language expression property array index '5' is out of bounds.. Please see https://aka.ms/arm-functions for usage details.'."}
So how you can use the mul function the get he number of total components and refer them in the copyIndex propertly? There isn´t on internet much examplex related to what I´m trying to get.
Thank you
CopyIndex() language expression property array index '5' is out of bounds:
After analysing the error, it appears that the issue is with the copyindex()
function in your code. The function copyindex()
returns the index of the current iteration of the copy()
loop. Array index out of bounds occurs when an array reaches its maximum limit and exceeds the index limit specified in the parameters
block.
To avoid this conflict, use ARM mod
& div
numeric functions in the name
property of private endpoints
resource.
After doing few modifications to your code, it worked as expected which is given below.
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"vnetArray": {
"type": "array",
"defaultValue": [
"Vnet-1ab-1",
"Vnet-2ab-8",
"Vnet-3ab-9",
"Vnet-4ab-34",
"Vnet-5ab"
]},
"subnetArray": {
"type": "array",
"defaultValue": ["Subet-a","Subnet-b","Subnet-c","Subnet-d"]
}
},
"variables":{
"privateEndpointName": "xxxxx"
},
"resources": [
{
"type": "Microsoft.Network/privateEndpoints",
"apiVersion": "2023-04-01",
"copy":{
"name": "peLoop",
"count": "[mul(length(parameters('vnetArray')), length(parameters('subnetArray')))]"
},
"name": "[concat(parameters('subnetArray')[mod(copyIndex('peLoop'), length(parameters('subnetArray')))], '-mongo-pe-', replace(parameters('vnetArray')[div(copyIndex('peLoop'), length(parameters('subnetArray')))], 'Vnet-', ''))]",
"location": "East US",
"properties": {
"privateLinkServiceConnections": [
{
"name": "[variables('privateEndpointName')]",
"properties": {
"privateLinkServiceId": "[resourceId('Microsoft.Network/privateEndpoints', variables('privateEndpointName'))]"
}
}
]
}
}]
}
Output: