0

I am trying to set diagnosticSettings to an array of databases in bicep using for (db, i) in dataBaseArray.

At first I create the databases:

@batchSize(1)
resource sqlDatabases 'Microsoft.Sql/servers/databases@2023-08-01' = [for db in dataBaseArray: {
  parent: sqlServer
  name: db.name
  location: location
  sku: {
    name: db.skuName
    tier: db.skuTier
  }
  tags: db.tags
  properties: {
    collation: db.collation
    isLedgerOn: db.isLedgerOn
    readScale: db.readScale
    requestedBackupStorageRedundancy: db.requestedBackupStorageRedundancy
    maxSizeBytes: db.maxSizeBytes
  }
}]

I tried setting it up like this

@batchSize(1)
resource sqlDiagnostics 'Microsoft.Insights/diagnosticSettings@2021-05-01-preview' = [for (db, i) in dataBaseArray: {
  scope: sqlDatabases[i]
  name: 'default'
  properties: {
    logAnalyticsDestinationType: 'AzureDiagnostics'
    workspaceId: db.logAnalyticsWorkspaceId
    logs: [
      {
        categoryGroup: 'allLogs'
        enabled: true
      }
      {
        categoryGroup: 'audit'
        enabled: true
      }
    ]
    metrics: [
      {
        category: 'Basic'
        enabled: true
      }
      {
        category: 'InstanceAndAppAdvanced'
        enabled: true
      }
      {
        category: 'WorkloadManagement'
        enabled: true
      }
    ]
  }
}]

However I always run into this issue:

Deployment template validation failed: 'The template resource 'Microsoft.Sql/servers/sql-server-tst/databases/db-tst/providers/Microsoft.Insights/diagnosticSettings/default' cannot reference itself

I am not sure why calling this module would cause a circular reference as it clearly targets a database resource and not a diagnosticSetting itself.

When I remove the loop and instead use scope: sqlDatabases[0] and dataBaseArray[0].logAnalyticsWorkspaceId the diagnosticSettings resource is created successfully. Why can't I set this up using the loop?

Should anyone be able to bring light into this darkness, I would very much appreciate it - thank you

1 Answer 1

2

I think this is some kind of a bug in Azure that is causing this. You can log bugs related to bicep at: https://github.com/Azure/bicep/issues. You can overcome this by specifying different name for each diagnostic setting. For example you can change the name of diagnostic settings to:

name: 'default-${db.name}'

Other option is to put diagnostic settings in separate module so you loop over the module. The module should deploy a single diagnostic setting for a single database.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.