title: Why can't I loop twice in an Azure resource module?
body: <p>I am trying to set up alerts in Azure bicep resources/modules. The goal is to have 3 files, one where the initial resource structure is created and parameterized:</p>
  <pre><code>param metricAlertsName string
  param description string
  param severity int
  param enabled bool = true
  param scopes array = []
  param evaluationFrequency string
  param windowSize string
  param targetResourceRegion string = resourceGroup().location
  param allOf array = []
  param actionGroupName string
  
  var actionGroupId = resourceId(resourceGroup().name, 'microsoft.insights/actionGroups', actionGroupName)
  
  resource dealsMetricAlerts 'Microsoft.Insights/metricAlerts@2018-03-01' = [for appService in appServices : {
    name: metricAlertsName
    location: 'global'
    tags: {}
    properties: {
      description: description
      severity: severity
      enabled: enabled
      scopes: scopes
      evaluationFrequency: evaluationFrequency
      windowSize: windowSize
      targetResourceRegion: targetResourceRegion
      criteria: {
        'odata.type': 'Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria'
        allOf: allOf
      }
      actions: [
        {
          actionGroupId: actionGroupId
        }
      ]
    }
  }]
  </code></pre>
  <p>A second file, main.bicep where all of my resources are being consumed as modules:</p>
  <pre><code>module dealsMetricAlerts '../modules/management/metric-alert.bicep' = [for (alert, index) in alertConfig.metricAlerts: {
    name: alert.name
    params: {
      metricAlertsName: alert.params.metricAlertsName
      actionGroupName: actionGroupName
      description: alert.params.description
      evaluationFrequency: alert.params.evaluationFrequency
      severity: alert.params.severity
      windowSize: alert.params.windowSize
      allOf: alert.params.allOf
      scopes: [
        resourceId(resourceGroup().name,'Microsoft.Web/sites', 'gbt-${env1}-${env}-${location}-webapp')
      ]
    }
  }]
  </code></pre>
  <p>And the third file which is a parameter file in which I can loop over, enter its values into the module in main.bicep to create multiple resources without having to write more than one module:</p>
  <pre><code>{
    &quot;$schema&quot;: &quot;https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#&quot;,
    &quot;contentVersion&quot;: &quot;1.0.0.0&quot;,
    &quot;parameters&quot;: {
      &quot;alertConfig&quot;: {
        &quot;value&quot;: {
          &quot;metricAlerts&quot;: [
            {
              &quot;name&quot;: &quot;403ErrorTest&quot;,
              &quot;params&quot;: {
                &quot;metricAlertsName&quot;: &quot;AppService403ErrorTest&quot;,
                &quot;actionGroupName&quot;: &quot;actionGroupName&quot;,
                &quot;description&quot;: &quot;Alert fires whenever the App Service throws a 403 error&quot;,
                &quot;evaluationFrequency&quot;: &quot;PT5M&quot;,
                &quot;severity&quot;: 2,
                &quot;windowSize&quot;: &quot;PT15M&quot;,
                &quot;allOf&quot;: [
                  {
                    &quot;name&quot;: &quot;403errorTest&quot;,
                    &quot;metricName&quot;: &quot;Http403&quot;,
                    &quot;dimensions&quot;: [],
                    &quot;operator&quot;: &quot;GreaterThan&quot;,
                    &quot;threshold&quot;: 0,
                    &quot;timeAggregation&quot;: &quot;Count&quot;
                  }
                ]
              }
            },
            {
              &quot;name&quot;: &quot;5XXErrorTest&quot;,
              &quot;params&quot;: {
               // Values for 500 error
              }
            }
          ]
        }
      }
    }
  }
  </code></pre>
  <p>I have a total of 5 app services, 1 web app and 4 function apps. If I was to hard code the name of the app service like above then that one web app is deployed with the 2 errors exactly as it should.</p>
  <p>The issue that I'm having is to try and use another loop on the scopes param to have the 2 errors attached to all 5 app services.</p>
  <p>I tried something like:</p>
  <pre><code>// Created an array with the names of the app services
  var appServices = [
    'dda-webApp'
    'dealservice-fnapp'
    'itemdata-fnapp'
    'refdata-fnapp'
    'userprofile-fnapp'
  ]
  
  // Update the scopes param to loop through the names\
  scopes: [for appService in appServices: [
    resourceId(resourceGroup().name,'Microsoft.Web/sites', 'gbt-${env1}-${env}-${location}-${appService}')
  ]]
  </code></pre>
  <p>This most often gives me an error saying: <code>Unexpected token array in scopes[0]</code>.</p>
  <p>I believe what's happening is when it's trying to look up the resource by name in resourceId it's looping through all of the appServices names and attaching the entire array onto the resource name making it invalid.</p>
  <p>Is there another way to loop through these scopes in order to attach the 403 and 500 alerts to each of my 5 app services?</p>