skip to Main Content

I’m getting an error "Cannot find VirtualNetwork with name vnet-name". The vnet is in a different resource group than the web app.

var subnetName = 'subnetName'
var linuxVersion = 'node|20-lts'

resource appService 'Microsoft.Web/sites@2023-12-01' = {
  name: '${webAppPrefix}${name}'
  location: location
  properties: {
    serverFarmId: webAppPlan.id
    publicNetworkAccess: 'Disabled'
    virtualNetworkSubnetId: resourceId('Microsoft.Network/virtualNetworks/subnets', 'vnet-name' , subnetName)
    siteConfig: {
      linuxFxVersion: linuxVersion  
    }
  }
}

2

Answers


  1. Chosen as BEST ANSWER

    Because the Virtual Network is in a different Resource Group as the app service, the Resource Group must be explicitly specified as follows:

    virtualNetworkSubnetId: resourceId('vnet-resource-group-name', 'Microsoft.Network/virtualNetworks/subnets','vnet-name', 'subnet-name')
    

    it is well documented here: https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/template-functions-resource


  2. Because the existing virtual network is in a different resource group, you need to pass the name of the resource group that it is in to the resourceId function.

    virtualNetworkSubnetId: resourceId('resourceGroupName', 'Microsoft.Network/virtualNetworks/subnets', 'vnet-name', 'subnet-name')
    

    Alternatively, use the existing keyword to work with your existing resources.

    resource existingVirtualNetwork 'Microsoft.Network/virtualNetworks@2023-11-01' existing = {
      name: 'vnet-name'
      scope: resourceGroup('resourceGroupName')
    }
    
    resource existingSubnet 'Microsoft.Network/virtualNetworks/subnets@2023-11-01' existing = {
      name: 'subnetName'
      parent: existingVirtualNetwork
    }
    
    resource appService 'Microsoft.Web/sites@2023-12-01' = {
      name: 'appservice-deployment'
      location: location
      properties: {
        virtualNetworkSubnetId: existingSubnet.id
        ...
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search