Azure Database SQL Firewall Rule

This page shows how to write Terraform and Azure Resource Manager for Database SQL Firewall Rule and write them securely.

azurerm_sql_firewall_rule (Terraform)

The SQL Firewall Rule in Database can be configured in Terraform with the resource name azurerm_sql_firewall_rule. The following sections describe 10 examples of how to use the resource and its parameters.

Example Usage from GitHub

firewall.tf#L1
resource "azurerm_sql_firewall_rule" "allow-azure-services" {
  depends_on = [
    azurerm_mssql_server.db-server
  ]
  count               = var.sql_firewall_allow_azure_services ? 1 : 0
  name                = "Allow Azure Services"
firewall.tf#L9
resource "azurerm_sql_firewall_rule" "sqlfirewall-myip" {
  name                = "personal-ip"
  resource_group_name = azurerm_resource_group.rg.name
  server_name         = azurerm_sql_server.sqlserver.name
  start_ip_address    = data.http.ip_address.body
  end_ip_address      = data.http.ip_address.body
main.tf#L10
resource "azurerm_sql_firewall_rule" "firewall_rule" {
  depends_on = [azurerm_sql_server.sql_server]

  name                = "InitializerIp"
  resource_group_name = var.resource_group
  server_name         = var.server_name
sql_server_firewall_no_inbound_all_infra.tf#L33
resource "azurerm_sql_firewall_rule" "validrule1" {
  name                = "validrule1"
  resource_group_name = azurerm_resource_group.example.name
  server_name         = azurerm_sql_server.example.name
  start_ip_address    = "10.0.17.62"
  end_ip_address      = "10.0.17.62"
sql_firewall_rules.tf#L2
resource "azurerm_sql_firewall_rule" "sql_azure_services" {
  name                = "azure_services"
  resource_group_name = var.rg_name
  server_name         = azurerm_sql_server.sql_server.name
  start_ip_address    = "0.0.0.0"
  end_ip_address      = "0.0.0.0"
synapse.tf#L15
resource "azurerm_sql_firewall_rule" "synapse_sql_server_fw_rule_drh" {
  count               = var.synapse_sql_server_count
  name                = "davids_home"
  resource_group_name = azurerm_resource_group.vnet_infra.name
  server_name         = azurerm_sql_server.synapse_sql_server[count.index].name
  start_ip_address    = data.azurerm_key_vault_secret.davids_home_ip.value
sql_firewall_rules.tf#L2
resource "azurerm_sql_firewall_rule" "sql_azure_services" {
  name                = "azure_services"
  resource_group_name = var.rg_name
  server_name         = azurerm_sql_server.sql_server.name
  start_ip_address    = "0.0.0.0"
  end_ip_address      = "0.0.0.0"
firewall.tf#L1
resource "azurerm_sql_firewall_rule" "azure_services" {
  name                = "azure-services"
  resource_group_name = var.resource_group
  server_name         = azurerm_mssql_server.this.name
  start_ip_address    = "0.0.0.0"
  end_ip_address      = "0.0.0.0"
main.tf#L25
resource "azurerm_sql_firewall_rule" "firewall_rule_good" {
  name                = "FirewallRule1"
  resource_group_name = azurerm_resource_group.example.name
  server_name         = azurerm_sql_server.sql_server_good.name
  start_ip_address    = "10.0.17.62"
  end_ip_address      = "10.0.17.62"
main.tf#L25
resource "azurerm_sql_firewall_rule" "firewall_rule_good" {
  name                = "FirewallRule1"
  resource_group_name = azurerm_resource_group.example.name
  server_name         = azurerm_sql_server.sql_server_good.name
  start_ip_address    = "10.0.17.62"
  end_ip_address      = "10.0.17.62"

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_sql_firewall_rule

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

risk-label

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.

Review your Azure Database settings

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

Parameters

Explanation in Terraform Registry

Allows you to manage an Azure SQL Firewall Rule

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_database_extended_auditing_policy

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.

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_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/firewallRules (Azure Resource Manager)

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

Example Usage from GitHub

Resources.SQL.json#L32
                "Type": "Microsoft.Sql/servers/firewallRules",
                "ResourceType": "Microsoft.Sql/servers/firewallRules",
                "SubscriptionId": "00000000-0000-0000-0000-000000000000"
            },
            {
                "ResourceId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/test-rg/providers/Microsoft.Sql/servers/server-A/securityAlertPolicies/Default",
db-server-template.json#L140
            "type": "Microsoft.Sql/servers/firewallRules",
            "apiVersion": "2020-08-01-preview",
            "name": "[concat(parameters('serverName'), '/ClientIPAddress_2020-12-29_05:08:24')]",
            "dependsOn": [
                "[resourceId('Microsoft.Sql/servers', parameters('serverName'))]"
            ],
azuredeployDB.json#L51
      "type": "Microsoft.Sql/servers/firewallRules",
      "apiVersion": "2021-02-01-preview",
      "name": "[concat(parameters('servers_ssnzdbserver_name'), '/150SouthPoint')]",
      "dependsOn": [
        "[resourceId('Microsoft.Sql/servers', parameters('servers_ssnzdbserver_name'))]"
      ],
FirewallRuleList.json#L15
            "type": "Microsoft.Sql/servers/firewallRules",
            "location": "Japan East",
            "kind": "v12.0",
            "properties": {
              "startIpAddress": "0.0.0.0",
              "endIpAddress": "0.0.0.0"
FirewallRuleList.json#L15
            "type": "Microsoft.Sql/servers/firewallRules",
            "properties": {
              "startIpAddress": "0.0.0.0",
              "endIpAddress": "0.0.0.0"
            }
          },
FirewallRuleList.json#L15
            "type": "Microsoft.Sql/servers/firewallRules",
            "location": "Japan East",
            "kind": "v12.0",
            "properties": {
              "startIpAddress": "0.0.0.0",
              "endIpAddress": "0.0.0.0"
FirewallRuleList.json#L15
            "type": "Microsoft.Sql/servers/firewallRules",
            "properties": {
              "startIpAddress": "0.0.0.0",
              "endIpAddress": "0.0.0.0"
            }
          },
FirewallRuleList.json#L15
            "type": "Microsoft.Sql/servers/firewallRules",
            "properties": {
              "startIpAddress": "0.0.0.0",
              "endIpAddress": "0.0.0.0"
            }
          },
FirewallRuleList.json#L15
            "type": "Microsoft.Sql/servers/firewallRules",
            "location": "Japan East",
            "kind": "v12.0",
            "properties": {
              "startIpAddress": "0.0.0.0",
              "endIpAddress": "0.0.0.0"
FirewallRuleList.json#L15
            "type": "Microsoft.Sql/servers/firewallRules",
            "location": "Japan East",
            "kind": "v12.0",
            "properties": {
              "startIpAddress": "0.0.0.0",
              "endIpAddress": "0.0.0.0"

Parameters

  • apiVersion required - string
  • name required - string

    The name of the firewall rule.

  • properties required
      • endIpAddress required - string

        The end IP address of the firewall rule. Must be IPv4 format. Must be greater than or equal to startIpAddress. Use value '0.0.0.0' to represent all Azure-internal IP addresses.

      • startIpAddress required - string

        The start IP address of the firewall rule. Must be IPv4 format. Use value '0.0.0.0' to represent all Azure-internal IP addresses.

  • type required - string

Frequently asked questions

What is Azure Database SQL Firewall Rule?

Azure Database SQL Firewall Rule 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 Firewall Rule?

For Terraform, the gayatri1030/azuresqlserver, higorbarbosa/treinamento-terraform-azure and Kakaranish/eszop-infra source code examples are useful. See the Terraform Example section for further details.

For Azure Resource Manager, the Azure/PSRule.Rules.Azure, Radical-Dave/Auto and SamSmithNZ-dotcom/SamSmithNZ.com source code examples are useful. See the Azure Resource Manager Example section for further details.