Review Azure SQL server auditing policies

Configure auditing to retain the database activity you need.

Description

Use a server audit policy for operations you need to record across databases on the same logical server. This includes example-db in the excerpts.

Potential impact

If the policy omits the relevant account activity or change, investigators may have no record to trace.

Remediation

Point azurerm_mssql_server_extended_auditing_policy at the intended server_id. Select the operations and destination. With log_monitoring_enabled set as shown, also route SQLSecurityAuditEvents from the master database to your Azure Monitor destination through diagnostic settings. Perform a selected operation and verify its record at each destination you use.

Examples

The example retains logs in Blob Storage for 90 days. Its primary_access_key requires a storage account that allows Shared Key authorization. If Shared Key is disabled, configure auditing with a SQL server managed identity that can write to the account. Manage production administrator credentials separately.

Before

hcl
resource "azurerm_mssql_server" "example" {
  name                         = "mssqlserver"
  resource_group_name          = azurerm_resource_group.example.name
  location                     = azurerm_resource_group.example.location
  version                      = "12.0"
  administrator_login          = "mradministrator"
  administrator_login_password = "thisIsDog11"
}

resource "azurerm_mssql_database" "example" {
  name      = "example-db"
  server_id = azurerm_mssql_server.example.id
}

After

hcl
resource "azurerm_mssql_server" "example" {
  name                         = "mssqlserver"
  resource_group_name          = azurerm_resource_group.example.name
  location                     = azurerm_resource_group.example.location
  version                      = "12.0"
  administrator_login          = "mradministrator"
  administrator_login_password = "thisIsDog11"
}

resource "azurerm_mssql_server_extended_auditing_policy" "example" {
  server_id                               = azurerm_mssql_server.example.id
  blob_storage_endpoint                        = azurerm_storage_account.example.primary_blob_endpoint
  storage_account_access_key              = azurerm_storage_account.example.primary_access_key
  storage_account_access_key_is_secondary = false
  retention_in_days                       = 90
  log_monitoring_enabled                  = true
}

resource "azurerm_mssql_database" "example" {
  name      = "example-db"
  server_id = azurerm_mssql_server.example.id
}

References