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