bicep icon indicating copy to clipboard operation
bicep copied to clipboard

Unsure of how to add diagnosticSettings to a master database object

Open jacobludriks opened this issue 5 years ago • 5 comments

I am looking at creating a SQL Server resource that sends audit information to a Log Analytics workspace. This is the current code:

resource logAnalyticsWorkspace 'Microsoft.OperationalInsights/workspaces@2020-10-01' = {
  name: resourceGroup().name
  location: resourceGroup().location
  properties: {
    sku: {
      name: 'PerGB2018'
    }
    retentionInDays: 365
  }
}

resource sqlServer 'Microsoft.Sql/servers@2021-02-01-preview' = {
  name: 'SQLServer'
  location: resourceGroup().location
  identity: {
    type: 'SystemAssigned'
  }
  properties: {
    administratorLogin: 'test'
    administratorLoginPassword: 'test'
    version: '12.0'
    minimalTlsVersion: '1.2'
    publicNetworkAccess: 'Disabled'
  }
}

resource sqlServerAuditing 'Microsoft.Sql/servers/auditingSettings@2021-02-01-preview' = {
  name: 'default'
  parent: sqlServer
  properties: {
    state: 'Enabled'
    retentionDays: 0
    auditActionsAndGroups: [
      'SUCCESSFUL_DATABASE_AUTHENTICATION_GROUP'
      'FAILED_DATABASE_AUTHENTICATION_GROUP'
      'BATCH_COMPLETED_GROUP'
    ]
    isAzureMonitorTargetEnabled: true
  }
  dependsOn: [
    logAnalyticsWorkspace
  ]
}

resource sqlServerAuditingDiagnosticSettings 'Microsoft.Insights/diagnosticSettings@2017-05-01-preview' = {
  name: 'SqlAuditing'
  scope: sqlServer
  properties: {
    workspaceId: logAnalyticsWorkspace.id
    logs: [
      {
        category: 'SQLSecurityAuditEvents'
        enabled: true
      }
    ]
  }
  dependsOn: [
    sqlServer
    logAnalyticsWorkspace
  ]
}

Thje documentation for this function says that the "scope" of the diagnosticSettings object should be in the following format:

https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.Sql/servers/{serverName}/databases/{databaseName}/providers/microsoft.insights/diagnosticSettings/{settingsName}?api-version=2017-05-01-preview

However I am unable to construct this for the scope. How would I go about this?

jacobludriks avatar Jun 10 '21 06:06 jacobludriks

https://github.com/Azure/bicep/tree/main/docs/examples/301/sql-database-with-management

slavizh avatar Jun 10 '21 08:06 slavizh

Thanks @slavizh . Is there a way to do this without sending a dummy request?

jacobludriks avatar Jun 10 '21 22:06 jacobludriks

@jacobludriks not at this time. My testing shows that when you create the SQL Logical Server there is some small delay until the master database resource appears.

slavizh avatar Jun 11 '21 06:06 slavizh

This solution works, so thank you for your help.

jacobludriks avatar Jun 17 '21 22:06 jacobludriks

It would be great to have the root cause of this issue solved or at least documented better - runtime pitfalls such as these make working with bicep or IaC a pain. Also, referencing the existing master database still makes the deployment fail during the first run because the existing keyword makes ARM try to resolve it immediately. It is impossible to put a dependsOn in an existing resource.

hansmbakker avatar Jan 04 '23 10:01 hansmbakker