How to resolve the following error for creating Azure NSG flow log with Traffic Analytics

Murali R 205 Reputation points
2023-12-21T16:40:01.7133333+00:00

Hi Team,
Receiving the following error while deploying NSG flow log with Traffic Analytics through BICEP template

##[error]Deployment template validation failed: 'The template resource '[format('{0}/Microsoft.Network{1}{2}', parameters('networkWatcherNames')[range(0, length(parameters('nsgIds')))[copyIndex()]], parameters('nsgRgNames')[range(0, length(parameters('nsgIds')))[copyIndex()]], parameters('nsgNames')[range(0, length(parameters('nsgIds')))[copyIndex()]])]' at line '1' and column '924' is not valid: The language expression property array index '3' is out of bounds..

Below is the BICEP template that iam trying to deploy. I see the error is caused due to Network Watcher and location parameters being an array. I have also tried modifying the below BICEP template where i have used those parameter name as string and the deployment was successful.
Please note that the networkWatcherNames and locations belong to NetworkWatcherRG and the rest of the parameters belong to NetworkSecurityGroupRG.
Iam trying to achieve Single BICEP template where i can use Single RG or multiple RG deployment also. Kindly help me with the error. Also i would like to know whether this requirement (having all the parameters as an array) is achievable.

param nsgRgNames array = [] // Your actual values
param nsgIds array = [] // Your actual values
param nsgNames array = [] // Your actual values
param networkWatcherNames array = [] // This is now an array of Network Watcher names
param locations array = [] // This is now an array of locations
param storageIds array = [] // This is now an array of storage account IDs

@description('Log analytics workspace resource Guid')
param workspaceId string = '' // Your actual value

@description('Log analytics workspace region')
param workspaceRegion string = '' // Your actual value

@description('Log analytics workspace resource id')
param workspaceResourceId string = '' // Your actual value

resource flowLogs 'Microsoft.Network/networkWatchers/flowLogs@2020-11-01' = [for i in range(0, length(nsgIds)): {
  name: '${i < length(networkWatcherNames) ? networkWatcherNames[i] : networkWatcherNames[0]}/Microsoft.Network${nsgRgNames[i]}${nsgNames[i]}'
  location: i < length(locations) ? locations[i] : locations[0]
  properties: {
    targetResourceId: nsgIds[i]
    storageId: storageIds[i]
    enabled: true
    format: {
      type: 'JSON'
      version: 2
    }
    retentionPolicy: {
      days: 0
      enabled: true
    }
    flowAnalyticsConfiguration: {
      networkWatcherFlowAnalyticsConfiguration: {
      enabled: true
      workspaceId: workspaceId
      workspaceRegion: workspaceRegion
      workspaceResourceId: workspaceResourceId
    }
  }
  tags: {
    'nsgRgName': nsgRgNames[i]
  }
  }
}]


Azure Network Watcher
Azure Network Watcher
An Azure service that is used to monitor, diagnose, and gain insights into network performance and health.
158 questions
{count} votes