In an ARM template in Azure, I am trying to create a load balancer attached to an existing VNet.
The issue is related to the location: the VNet and the resource groups are not in the same location, and the load balancer can only be created in the same location as the VNet (which make sense), so I can't use 'resourceGroup().location'.
According to reference function's doc, I can use it in the definition of a new resource, but when I define the location from the load balancer with the reference to the VNet, I get an error.
Extract from the definition:
"resources": [
{
"type": "Microsoft.Network/loadBalancers",
"apiVersion": "2020-06-01",
"name": "[parameters('loadBalancerName')]",
"location": "[reference(resourceId('Microsoft.Network/VirtualNetworks',parameters('vNetName')),'2022-01-01','Full').location]",
"sku": {
"name": "Standard"
},
"properties": {
Previously, I was using 'resourceGroup().location' as the VNet were always in the same location as the resource group.
With this code, I get the following error:
{"code": "InvalidTemplate", "message": "Deployment template validation failed: 'The template resource 'mytemplate' at line '28' and column '5' is not valid: The template function 'reference' is not expected at this location. Please see https://aka.ms/arm-template-expressions for usage details.. Please see https://aka.ms/arm-template-expressions for usage details.'.", "additionalInfo": [{"type": "TemplateViolation", "info": {"lineNumber": 28, "linePosition": 5, "path": "properties.template.resources[0]"}}]}
I don't want to specify as parameter the VNet location, as it already exists, and I should be table to retrieve it from the name I'm already providing.
When I try to use the reference function in variables, I get an error it can't be used in variables.
