Kathrine Stack
Kathrine Stack

Reputation: 319

Using copyindex() for subnet name creates error as a duplicate resource

I've tried using copyIndex() to create subnets with different names but I get the error

        "message": "Resource
/subscriptions//resourceGroups//providers/Microsoft.Network/virtualNetworks/ has two child
resources with the same name
([parameters('subnets').subnetProperties[copyIndex('subnets')].name)).

But I followed documation to use copy and this is what I've been using, so I'm not sure why is wouldn't move to the next name property:

 "resources": [
    {
        "type": "Microsoft.Network/virtualNetworks",
        "apiVersion": "2016-03-30",
        "name": "[parameters('virtualNetworkName')]",
        "location": "[parameters('location')]",
        "tags": "[parameters('virtualNetworkTags')]",
        "properties": {
        "addressSpace": {
            "addressPrefixes": [
             "[parameters('vNetAddressSpaces')]"
          ]
        },
            "copy": [
                {
                    "name": "subnets",
                    "count": "[parameters('numberOfSubnets')]",
                    "input": {
                        "name": "[parameters('subnets').subnetProperties[copyIndex('subnets')].name)",
                        "properties": {
                            "addressPrefix": "[parameters('subnets').subnetProperties[copyIndex('subnets')].addressPrefix]"
                        }
                    }
                }
            ]
        }
    },

Param file:

"subnets":{
  "value":{
    "subnetProperties":[
      {
        "name":"firstSubnet",
        "addressPrefix":"10.0.0.0/24"
      },
      {
        "name":"secondSubnet",
        "addressPrefix":"10.0.1.0/24"
      }
    ]        
  }
},

I've also tried using copyIndex(), but that throws

 template language expression evaluation failed: 'The template language
function 'copyIndex' has an invalid argument. The provided copy name '' doesn't exist in the
resource.

Upvotes: 0

Views: 1028

Answers (1)

KirKone
KirKone

Reputation: 603

I think you messed up with the brackets in this line:

"name": "[parameters('subnets').subnetProperties[copyIndex('subnets')].name)",

It should look like:

"name": "[parameters('subnets').subnetProperties[copyIndex('subnets')].name]",

The last bracket is wrong. If the brackets does not match the complete expression will not processed. This will result in the same name on the second loop.

Greetings, KirK

Upvotes: 1

Related Questions