Kamsiinov
Kamsiinov

Reputation: 1486

Azure ARM template 'copyIndex' is not expected when applying NSG to subnet

I am creating NSGs with copy function and it works fine. However, I would like to apply the NSGs similar way to the subnets but I get the copyindex not expected.

{
  "apiVersion": "2017-08-01",
  "name": "apply-nsg-to-subnet",
  "type": "Microsoft.Resources/deployments",
  "properties": {
    "mode": "Incremental",
    "template": {
      "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
      "contentVersion": "#{BuildNumber}#",
      "resources": [
        {
          "apiVersion": "2018-06-01",
          "type": "Microsoft.Network/virtualNetworks/subnets",
          "name": "[concat(parameters('vnetName') , '/' , parameters('subnets').settings[copyIndex()].name)]",
          "location": "[variables('location')]",
          "copy":{
            "name": "subnetLoop",
            "count": "[variables('subnetcount')]",
            "mode": "Serial"
          },
          "properties": {
            "addressPrefix": "[parameters('subnets').settings[copyIndex()].addressPrefix]",
            "networkSecurityGroup": {
              "id": "[resourceId('Microsoft.Network/networkSecurityGroups', concat(parameters('nsgNameAffix'), parameters('subnets').settings[copyIndex()].name, variables('nsgNameSuffix')))]"
            }
          }
        }
      ]
    }
  }
}

What is wrong with my copyIndex use and how it should be used in this case?

Upvotes: 1

Views: 1781

Answers (1)

4c74356b41
4c74356b41

Reputation: 72191

This is due to the nested inline template you are using, I was able to repro and I was able to work around with this sample template:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "subnets": {
            "type": "array",
            "defaultValue": [
                {
                    "name": "testo",
                    "addressPrefix": "10.0.0.0/24"
                },
                {
                    "name": "testo1",
                    "addressPrefix": "10.0.1.0/24"
                }
            ]
        }
    },
    "resources": [
        {
            "apiVersion": "2018-08-01",
            "name": "vnet-testo",
            "type": "Microsoft.Network/virtualNetworks",
            "location": "[resourceGroup().location]",
            "properties": {
                "addressSpace": {
                    "addressPrefixes": [
                        "10.0.0.0/16"
                    ]
                }
            }
        },
        {
            "apiVersion": "2015-06-15",
            "type": "Microsoft.Network/networkSecurityGroups",
            "name": "[parameters('subnets')[copyIndex()].name]",
            "location": "[resourceGroup().location]",
            "copy": {
                "name": "nsg",
                "count": "[length(parameters('subnets'))]"
            },
            "properties": {
                "securityRules": []
            }
        },
        {
            "name": "NestedDeployment1",
            "type": "Microsoft.Resources/deployments",
            "apiVersion": "2015-01-01",
            "dependsOn": [
                "nsg",
                "vnet-testo"
            ],
            "properties": {
                "mode": "Incremental",
                "templateLink": {
                    "uri": "https://paste.ee/d/iCWEu/0",
                    "contentVersion": "1.0.0.0"
                },
                "parameters": {
                    "subnets": {
                        "value": "[parameters('subnets')]"
                    }
                }
            }
        }
    ]
}

Basically, I've just converted your template to a nested template (not inline).

ps. checkout my copy definition, its a bit better then yours.

Upvotes: 1

Related Questions