azure-rm-templateazure-virtual-networkvnetazure-public-ip

ARM Template - Creating a template for VirtualnetworkGateway combined in a single template


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.


Solution

  • 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'))]"
              },