azureazure-resource-managerazure-synapseazure-bicep

Bicep How to reference nested child resource in a for loop?


How to add Diagnostic Settings into a given Spark Pool, which means is a provider from another provider and we have more than one Spark Pool.

What I tried:

resource diagnosticsSynapseWorkspaceSparkPools 'Microsoft.Insights/diagnosticSettings@2021-05-01-preview' = [
  for sparkPool in sparkPools: {
    name: '${sparkPool}diag'
    scope: resourceId('Microsoft.Synapse/workspaces/bigDataPools', synapseWorkspace.name, sparkPool)
    properties: {
      workspaceId: operationalInsightsWorkspace.id
      logs: [
        {
          category: null
          categoryGroup: 'allLogs'
          enabled: true
          retentionPolicy: {
            days: 0
            enabled: false
          }
        }
      ]
    }
  }
]

But I get the error: Values of the "any" type are not allowed here.bicepBCP176 for the resourceId('Microsoft.Synapse/workspaces/bigDataPools', synapseWorkspace.name, sparkPool).

If I do this scope: '/subscriptions/${subscriptionId}/resourceGroups/${resourceGroupName}/providers/Microsoft.Synapse/workspaces/${synapseWorkspace.name}/bigDataPools/${sparkPool}' I also get another error The property "scope" expected a value of type "resource | tenant" but the provided value is of type "string".bicepBCP036

What other way could I reference the spark pool id?


Solution

  • For diagnostic settings, the scope needs to be a resource. So you could get the existing big data pools resources and iterate this way:

    resource sparkPoolResources 'Microsoft.Synapse/workspaces/bigDataPools@2021-06-01' existing = [
      for sparkPool in sparkPools: {
        name: sparkPool
        parent: synapseWorkspace
      }
    ]
    
    resource diagnosticsSynapseWorkspaceSparkPools 'Microsoft.Insights/diagnosticSettings@2021-05-01-preview' = [
      for (sparkPool, i) in sparkPools: {
        name: '${sparkPool}diag'
        scope: sparkPoolResources[i]
        properties: {
          workspaceId: operationalInsightsWorkspace.id
          logs: [
            {
              category: null
              categoryGroup: 'allLogs'
              enabled: true
              retentionPolicy: {
                days: 0
                enabled: false
              }
            }
          ]
        }
      }
    ]