0

I am trying to conditionally deploy a route template when deploying a subnet resource using an ARM template, however, I am not able to do so using the if condition. Would anyone know the correct syntax or what am I doing wrong?

{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
    "routeTableConfigurationObject": {
        "type": "object",
        "defaultValue": null
    },
    "vnetConfigurationObject": {
        "type": "object"
    },
    "Tags": {
        "type": "object"
    }
},
"resources": [
    {
        "type": "Microsoft.Network/virtualNetworks/Subnets",
        "name": "[concat(parameters('vnetConfigurationObject').name,'/',parameters('vnetConfigurationObject').subnets[copyIndex()].name)]",
        "location": "[parameters('vnetConfigurationObject').location]",
        "apiVersion": "2018-08-01",
        "copy": {
            "name": "vnet",
            "count": "[length(parameters('vnetConfigurationObject').subnets)]",
            "mode": "serial"
        },
        "properties": {
            "addressPrefix": "[parameters('vnetConfigurationObject').subnets[copyIndex()].addressPrefix]",
            "routeTable": "[if(empty('routeTableConfigurationObject'), json('null'), json(concat('{\"id\": \"', '/subscriptions/', parameters('routeTableConfigurationObject').subscriptionId, '/resourceGroups/', parameters('routeTableConfigurationObject').resourceGroupName, '/providers/Microsoft.Network/routeTables/', parameters('vnetConfigurationObject').subnets[copyIndex()].routeTable,'\"}')))]"
        }
    }
]

}

Akash Masand
  • 1,441
  • 14
  • 30

1 Answers1

0

try this:

"variables": [
    "copy": [
        {
            "name": "route",
            "count": "[length(parameters('vnetConfigurationObject').subnets)]",
            "input": {
                "id": "[resourceId(parameters('routeTableConfigurationObject').subscriptionId, parameters('routeTableConfigurationObject').resourceGroupName, 'Microsoft.Network/routeTables', parameters('vnetConfigurationObject').subnets[copyIndex('route')].routeTable)]"
            }
        }
    ]
],
"resources": [
    {
        "type": "Microsoft.Network/virtualNetworks/Subnets",
        "name": "[concat(parameters('vnetConfigurationObject').name,'/',parameters('vnetConfigurationObject').subnets[copyIndex()].name)]",
        "location": "[parameters('vnetConfigurationObject').location]",
        "apiVersion": "2018-08-01",
        "copy": {
            "name": "vnet",
            "count": "[length(parameters('vnetConfigurationObject').subnets)]",
            "mode": "serial"
        },
        "properties": {
            "addressPrefix": "[parameters('vnetConfigurationObject').subnets[copyIndex()].addressPrefix]",
            "routeTable": "[if(empty('routeTableConfigurationObject'), json('null'), variables('route')[copyIndex()])]"
        }
    }
]

I dont have time\ability to test it, but it should be pretty close

4c74356b41
  • 69,186
  • 6
  • 100
  • 141