0

I have created a template which is meant to deploy a complete network solution, which includes 2-subnets, vnet, vnetgw and pubip. I am looking for a way to programmatically reference some of the resource id's such that it makes the template more dynamic and can be used as many times as possible. secondly, the templates generates an error on deployment which obviously is as a result of the the references i mentioned earlier. Pls see error below;

New-AzResourceGroupDeployment: Line | 3 | New-AzResourceGroupDeployment -ResourceGroupName rg-vnet-dev -Templat … | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 19:05:58 - Resource Microsoft.Network/virtualNetworkGateways 'rgvnetdev-vnetgw' failed with message '{ "error": { "code": "InvalidTemplate", "message": "Unable to process template language expressions for resource '/subscriptions/77dd2569-6341-4c72-880d-ef59413db99e/resourceGroups/rg-vnet-dev/providers/Microsoft.Network/virtualNetworkGateways/rgvnetdev-vnetgw' at line '279' and column '9'. 'Unable to evaluate template language function 'resourceId': the type 'Microsoft.Network/virtualNetworks/subnets' requires '2' resource name argument(s). Please see https://aka.ms/arm-template-expressions/#resourceid for usage details.'", "additionalInfo": [ { "type": "TemplateViolation", "info": { "lineNumber": 279, "linePosition": 9, "path": "" } } ] } }

I will be happy to share the code, if this would assist in resolving my issue. The error relates to referencing the vnet dependson for creating vnetgw.

Nancy
  • 26,865
  • 3
  • 18
  • 34

2 Answers2

1

From the error message, it seems that referenced subnet id is invalid. This function resourceId format is

resourceId([subscriptionId], [resourceGroupName], resourceType, resourceName1, [resourceName2], ...)

In this case, you probably lacks the VNet name at line '279', the referenced subnet id should be like this:

 "subnet": {
            "id": "[resourceId('Microsoft.Network/virtualNetworks/subnets', parameters('virtualNetworkName'), parameters('subnet1Name'))]"
          },
Nancy
  • 26,865
  • 3
  • 18
  • 34
  • Thanks @Nancy, the function i am trying to create is a dependency for creating a vnetgw to ensure that there is a vnet in place before creating the vnetgw."dependsOn": [ "[resourceId(subscription(),resourceGroup(),'Microsoft.Network/virtualNetworks',variables('VNetName'))]" ], this is what i was able to deduce from your explanation in first part of your answer. Can you please guide me on the right path. The other code about the subnet is perfect. – Olamide May 28 '20 at 11:25
  • Can you show your full code then I will check it tomorrow – Nancy May 28 '20 at 11:26
  • Hi @Nancy, I finally fixed the problem with my code by referencing the resources correctly as well as using intellisence from vscode – Olamide May 28 '20 at 17:26
0
       {
       "type": "Microsoft.Network/virtualNetworkGateways",
       "apiVersion": "2019-12-01",
       "name": "[variables('vnetgwname')]",
       "location": "[parameters('Location')]",
       "dependsOn": [
           "[resourceId('Microsoft.Network/publicIPAddresses',variables('pubIp'))]",
           "[resourceId('Microsoft.Network/virtualNetworks',variables('VNetName'))]"
       ],
       "properties": {
           "ipConfigurations": [
               {
                   "name": "vnetgatewayconfig",
                   "properties": {
                       "publicIPAddress": {
                           "id": "[resourceId('Microsoft.Network/publicIPAddresses',variables('pubIp'))]"
                       },
                       "subnet": {
                           "id": "[resourceId('Microsoft.Network/virtualNetworks/subnets',variables('VNetName'),variables('Uniquegwsubnet'))]"
                       },
                       "privateIPAllocationMethod": "[parameters('publicIPAllocationMethod')]"
                   }
               }
           ],
           "sku": {
               "name": "[parameters('sku')]",
               "tier": "[parameters('sku')]"
           },
           "gatewayType": "[parameters('gatewayType')]",
           "vpnType": "[parameters('VpnType')]",
           "activeActive": false,
           "enableBgp": false,
           "vpnGatewayGeneration": "[parameters('vpnGatewayGeneration')]"
       }
   },