Azure Database SQL Server Database Extended Auditing Policy

This page shows how to write Terraform and Azure Resource Manager for Database SQL Server Database Extended Auditing Policy and write them securely.

azurerm_mssql_database_extended_auditing_policy (Terraform)

The SQL Server Database Extended Auditing Policy in Database can be configured in Terraform with the resource name azurerm_mssql_database_extended_auditing_policy. The following sections describe 10 examples of how to use the resource and its parameters.

Example Usage from GitHub

main.tf#L1
resource "azurerm_mssql_database_extended_auditing_policy" "mssqldbaudit" {
  database_id                             = var.sqldb_database_id
  storage_endpoint                        = var.sqldb_audit_endpoint
  storage_account_access_key              = var.sqldb_audit_access_key
  storage_account_access_key_is_secondary = var.sqldb_audit_access_key_is_2nd
  retention_in_days                       = var.sqldb_audit_retention
main.tf#L16
resource "azurerm_mssql_database_extended_auditing_policy" "sql_db_ext_auditing_policys" {
  for_each = var.sql_server_db_names

  database_id                             = azurerm_mssql_database.sql_databases[each.key].id
  storage_endpoint                        = var.storage_account_blob_endpoint
  storage_account_access_key              = var.storage_account_primary_secret
azurerm_mssql_server_extended_auditing_policy.this.tf#L1
resource "azurerm_mssql_database_extended_auditing_policy" "example" {
  database_id                             = azurerm_mssql_database.examplea.id
  storage_endpoint                        = azurerm_storage_account.examplea.primary_blob_endpoint
  storage_account_access_key              = azurerm_storage_account.examplea.primary_access_key
  storage_account_access_key_is_secondary = false
  retention_in_days                       = var.audit_retention_in_days
main.tf#L36
resource "azurerm_mssql_database_extended_auditing_policy" "lab01" {
    database_id                             = azurerm_mssql_database.lab01.id
    storage_endpoint                        = azurerm_storage_account.lab01.primary_blob_endpoint
    storage_account_access_key              = azurerm_storage_account.lab01.primary_access_key
    storage_account_access_key_is_secondary = false
    retention_in_days                       = 30
auditing.tf#L21
resource "azurerm_mssql_database_extended_auditing_policy" "auditing" {
  depends_on = [
    azurerm_mssql_server.db-server,
    azurerm_mssql_database.db,
    azurerm_mssql_elasticpool.Vcore,
    azurerm_mssql_elasticpool.DTU
azurerm_mssql_server_extended_auditing_policy.this.tf#L1
resource "azurerm_mssql_database_extended_auditing_policy" "example" {
  database_id                             = azurerm_sql_database.examplea.id
  storage_endpoint                        = azurerm_storage_account.examplea.primary_blob_endpoint
  storage_account_access_key              = azurerm_storage_account.examplea.primary_access_key
  storage_account_access_key_is_secondary = false
  retention_in_days                       = var.audit_retention_in_days
main.tf#L34
resource "azurerm_mssql_database_extended_auditing_policy" "mssqldb" {
  database_id            = azurerm_mssql_database.sql_db.id
  storage_endpoint           = var.sa_primary_blob_endpoint
  storage_account_access_key = var.sa_primary_access_key

  retention_in_days      = var.retention_days
main.tf#L48
resource "azurerm_mssql_database_extended_auditing_policy" "lab02" {
    database_id                             = azurerm_mssql_database.lab02.id
    storage_endpoint                        = azurerm_storage_account.lab02.primary_blob_endpoint
    storage_account_access_key              = azurerm_storage_account.lab02.primary_access_key
    storage_account_access_key_is_secondary = false
    retention_in_days                       = 30
main.tf#L63
resource "azurerm_mssql_database_extended_auditing_policy" "example" {
  database_id                             = azurerm_mssql_database.test.id
  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                       = 6
main.tf#L34
resource "azurerm_mssql_database_extended_auditing_policy" "mssqldb" {
  database_id            = azurerm_mssql_database.sql_db.id
  storage_endpoint           = var.sa_primary_blob_endpoint
  storage_account_access_key = var.sa_primary_access_key

  retention_in_days      = var.retention_days

Review your Terraform file for Azure best practices

Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).

Security Best Practices for azurerm_mssql_database_extended_auditing_policy

There is 1 setting in azurerm_mssql_database_extended_auditing_policy that should be taken care of for security reasons. The following section explain an overview and example code.

risk-label

Ensure to configure retention periods of database auditing to enough duration

It is better to configure retention periods of database auditing to enough duration. It would be better to set greater than at least 90 days.

Review your Azure Database settings

You can check if the azurerm_mssql_database_extended_auditing_policy setting in your .tf file is correct in 3 min with Shisho Cloud.

Parameters

Explanation in Terraform Registry

Manages a Ms Sql Database Extended Auditing Policy.

NOTE: The Database Extended Auditing Policy Can be set inline here as well as with the mssql_database_extended_auditing_policy resource resource. You can only use one or the other and using both will cause a conflict.

Tips: Best Practices for The Other Azure Database Resources

In addition to the azurerm_mariadb_firewall_rule, Azure Database has the other resources that should be configured for security reasons. Please check some examples of those resources and precautions.

risk-label

azurerm_mariadb_firewall_rule

Ensure database firewalls do not permit public access

It is better to restrict IP address ranges that can access the database by firewall rules. If both start_ip_address and end_ip_address are set to 0.0.0.0, it blocks connections from the Internet and accepts connections from all Azure datacenter IP addresses.

risk-label

azurerm_mariadb_server

Ensure that access to Azure SQL Database is restricted

It is better to disable public access to the database to avoid unwilling communications with unknown services if not required.

risk-label

azurerm_mssql_server

Ensure to enable auditing on Azure SQL databases

It is better to enable auditing on Azure SQL databases. It helps you maintain regulatory compliance, monitor the activities indicating unexpected incidents or suspected security violations.

risk-label

azurerm_mssql_server_security_alert_policy

Ensure to configure at least one email address for threat alerts

It is better to configure at least one email address for threat alerts. SQL Server is able to send alerts for threat detection via emails and it could support us to notice the incident on time.

risk-label

azurerm_mysql_firewall_rule

Ensure database firewalls do not permit public access

It is better to restrict IP address ranges that can access the database by firewall rules. If both start_ip_address and end_ip_address are set to 0.0.0.0, it blocks connections from the Internet and accepts connections from all Azure datacenter IP addresses.

risk-label

azurerm_mysql_server

Ensure to disable public access to database

It is better to disable public access to the database to avoid unwilling communications with unknown services if not required.

risk-label

azurerm_postgresql_firewall_rule

Ensure database firewalls do not permit public access

It is better to restrict IP address ranges that can access the database by firewall rules. If both start_ip_address and end_ip_address are set to 0.0.0.0, it blocks connections from the Internet and accepts connections from all Azure datacenter IP addresses.

risk-label

azurerm_postgresql_server

Ensure to disable public access to database

It is better to disable public access to the database to avoid unwilling communications with unknown services if not required.

risk-label

azurerm_sql_firewall_rule

Ensure database firewalls do not permit public access

It is better to restrict IP address ranges that can access the database by firewall rules. If both start_ip_address and end_ip_address are set to 0.0.0.0, it blocks connections from the Internet and accepts connections from all Azure datacenter IP addresses.

risk-label

azurerm_sql_server

Ensure to enable auditing on Azure SQL databases

It is better to enable auditing on Azure SQL databases. It helps you maintain regulatory compliance, monitor the activities indicating unexpected incidents or suspected security violations.

Review your Azure Database settings

In addition to the above, there are other security points you should be aware of making sure that your .tf files are protected in Shisho Cloud.

Microsoft.Sql/servers/databases/extendedAuditingSettings (Azure Resource Manager)

The servers/databases/extendedAuditingSettings in Microsoft.Sql can be configured in Azure Resource Manager with the resource name Microsoft.Sql/servers/databases/extendedAuditingSettings. The following sections describe how to use the resource and its parameters.

Example Usage from GitHub

ExtendedDatabaseAzureMonitorAuditingCreateMin.json#L21
        "type": "Microsoft.Sql/servers/databases/extendedAuditingSettings",
        "properties": {
            "state": "Enabled",
            "isAzureMonitorTargetEnabled": true,
            "retentionDays": 0,
            "storageAccountSubscriptionId": "00000000-0000-0000-0000-000000000000",
ExtendedDatabaseAzureMonitorAuditingCreateMin.json#L21
        "type": "Microsoft.Sql/servers/databases/extendedAuditingSettings",
        "properties": {
            "state": "Enabled",
            "isAzureMonitorTargetEnabled": true,
            "retentionDays": 0,
            "storageAccountSubscriptionId": "00000000-0000-0000-0000-000000000000",
ExtendedDatabaseBlobAuditingCreateMin.json#L22
        "type": "Microsoft.Sql/servers/databases/extendedAuditingSettings",
        "properties": {
            "state":"Enabled",
            "storageEndpoint": "https://mystorage.blob.core.windows.net",
            "retentionDays": 0,
            "storageAccountSubscriptionId": "00000000-0000-0000-0000-000000000000",
ExtendedDatabaseBlobAuditingCreateMin.json#L22
        "type": "Microsoft.Sql/servers/databases/extendedAuditingSettings",
        "properties": {
            "state":"Enabled",
            "storageEndpoint": "https://mystorage.blob.core.windows.net",
            "retentionDays": 0,
            "storageAccountSubscriptionId": "00000000-0000-0000-0000-000000000000",
ExtendedDatabaseBlobAuditingCreateMin.json#L22
        "type": "Microsoft.Sql/servers/databases/extendedAuditingSettings",
        "properties": {
            "state":"Enabled",
            "storageEndpoint": "https://mystorage.blob.core.windows.net",
            "retentionDays": 0,
            "storageAccountSubscriptionId": "00000000-0000-0000-0000-000000000000",
ExtendedDatabaseAzureMonitorAuditingCreateMin.json#L21
        "type": "Microsoft.Sql/servers/databases/extendedAuditingSettings",
        "properties": {
            "state": "Enabled",
            "isAzureMonitorTargetEnabled": true,
            "retentionDays": 0,
            "storageAccountSubscriptionId": "00000000-0000-0000-0000-000000000000",
ExtendedDatabaseBlobAuditingCreateMin.json#L22
        "type": "Microsoft.Sql/servers/databases/extendedAuditingSettings",
        "properties": {
            "state":"Enabled",
            "storageEndpoint": "https://mystorage.blob.core.windows.net",
            "retentionDays": 0,
            "storageAccountSubscriptionId": "00000000-0000-0000-0000-000000000000",
ExtendedDatabaseBlobAuditingCreateMax.json#L27
        "type": "Microsoft.Sql/servers/databases/extendedAuditingSettings",
        "properties": {
            "state":"Enabled",
            "storageEndpoint": "https://mystorage.blob.core.windows.net",
            "retentionDays": 0,
            "storageAccountSubscriptionId": "00000000-1234-0000-5678-000000000000",
ExtendedDatabaseAzureMonitorAuditingCreateMin.json#L21
        "type": "Microsoft.Sql/servers/databases/extendedAuditingSettings",
        "properties": {
          "state": "Enabled",
          "isAzureMonitorTargetEnabled": true,
          "retentionDays": 0,
          "storageAccountSubscriptionId": "00000000-0000-0000-0000-000000000000",
ExtendedDatabaseAzureMonitorAuditingCreateMin.json#L21
        "type": "Microsoft.Sql/servers/databases/extendedAuditingSettings",
        "properties": {
            "state": "Enabled",
            "isAzureMonitorTargetEnabled": true,
            "retentionDays": 0,
            "storageAccountSubscriptionId": "00000000-0000-0000-0000-000000000000",

Frequently asked questions

What is Azure Database SQL Server Database Extended Auditing Policy?

Azure Database SQL Server Database Extended Auditing Policy is a resource for Database of Microsoft Azure. Settings can be wrote in Terraform.

Where can I find the example code for the Azure Database SQL Server Database Extended Auditing Policy?

For Terraform, the prancer-io/prancer-terramerra, logamanig/infinite.cloud.deployment and JamesWoolfenden/terraform-azurerm-mssqlserver source code examples are useful. See the Terraform Example section for further details.

For Azure Resource Manager, the assing/alerts-extension, shawns1/shawns1CI and yuncmsft/swagger source code examples are useful. See the Azure Resource Manager Example section for further details.