Azure Database SQL Server Database

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

azurerm_mssql_database (Terraform)

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

Example Usage from GitHub

AKS-MSSQL.tf#L25
resource "azurerm_mssql_database" "fitverse_ms_db" {
  name           = "-"
  server_id      = azurerm_sql_server.fitverse-sql-server.id
  sku_name       = "S0"

  extended_auditing_policy {
mssql_database_test.tf#L20
resource "azurerm_mssql_database" "general_purpose_gen_without_license" {
  name         = "acctest-db-d"
  server_id    = azurerm_sql_server.example.id
  sku_name     = "GP_Gen5_4"
  license_type = "BasePrice"
}
sql.tf#L134
resource "azurerm_mssql_database" "dtu_zr_not_eligible" {
  name           = "dtu-no-zr-db"
  server_id      = azurerm_mssql_server.aad_admin_only.id
  collation      = "SQL_Latin1_General_CP1_CI_AS"
  #license_type   = "LicenseIncluded"
  #max_size_gb    = 4
negative.tf#L1
resource "azurerm_mssql_database" "negative1" {
  name                = "myexamplesqldatabase"
  resource_group_name = azurerm_resource_group.example.name
  location            = "West US"
  server_name         = azurerm_sql_server.example.name

mssql_database_test.tf#L20
resource "azurerm_mssql_database" "general_purpose_gen_without_license" {
  name         = "acctest-db-d"
  server_id    = azurerm_sql_server.example.id
  sku_name     = "GP_Gen5_4"
  license_type = "BasePrice"
}
positive.tf#L1
resource "azurerm_mssql_database" "positive1" {
  name                = "myexamplesqldatabase"
  resource_group_name = azurerm_resource_group.example.name
  location            = "West US"
  server_name         = azurerm_mssql_server.example.name

negative.tf#L1
resource "azurerm_mssql_database" "negative1" {
  name                = "myexamplesqldatabase"
  resource_group_name = azurerm_resource_group.example.name
  location            = "West US"
  server_name         = azurerm_sql_server.example.name

main.tf#L16
resource "azurerm_mssql_database" "lab01" {
    name = "lab01-db"
    server_id = azurerm_mssql_server.lab01.id
}

resource "azurerm_mssql_database" "lab02" {
positive.tf#L1
resource "azurerm_mssql_database" "positive1" {
  name                = "myexamplesqldatabase"
  resource_group_name = azurerm_resource_group.example.name
  location            = "West US"
  server_name         = azurerm_mssql_server.example.name

db.tf#L30
resource "azurerm_mssql_database" "toscadb" {
  name                        = var.tosca_database_name
  server_id                   = azurerm_mssql_server.sql_server.id
  collation                   = "SQL_Latin1_General_CP1_CI_AS"
  license_type                = "LicenseIncluded"
  max_size_gb                 = var.sql_max_size

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).

Parameters

Explanation in Terraform Registry

Manages a MS SQL Database.

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

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

Example Usage from GitHub

test-db.template.json#L109
            "type": "Microsoft.Sql/servers/databases",
            "apiVersion": "2019-06-01-preview",
            "name": "[concat(parameters('servers_srvtestdb001_name'), '/testdb001')]",
            "location": "canadacentral",
            "dependsOn": [
                "[resourceId('Microsoft.Sql/servers', parameters('servers_srvtestdb001_name'))]"
v1.6_Export.json#L13
      "type": "Microsoft.Sql/servers/databases",
      "apiVersion": "2019-06-01-preview",
      "name": "[concat(parameters('servers_platformsupport01_name'), '/OrchestrationSupport')]",
      "location": "uksouth",
      "sku": {
        "name": "Standard",
db-template.json#L13
      "type": "Microsoft.Sql/servers/databases",
      "apiVersion": "2020-08-01-preview",
      "name": "[concat(parameters('servers_cadence_name'), '/ProductDB')]",
      "location": "australiaeast",
      "tags": {
        "ms-resource-usage": "cadence_rg"
template.json#L13
            "type": "Microsoft.Sql/servers/databases",
            "apiVersion": "2019-06-01-preview",
            "name": "[concat(parameters('servers_sap_server_name'), '/sap-db-dev')]",
            "location": "centralus",
            "sku": {
                "name": "Basic",
v1.6_Export.json#L13
      "type": "Microsoft.Sql/servers/databases",
      "apiVersion": "2019-06-01-preview",
      "name": "[concat(parameters('servers_platformsupport01_name'), '/OrchestrationSupport')]",
      "location": "uksouth",
      "sku": {
        "name": "Standard",
integration.json
{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "deploymentId": {
template.json#L117
            "type": "Microsoft.Sql/servers/databases",
            "apiVersion": "2021-02-01-preview",
            "name": "[concat(parameters('servers_azuresql2019_name'), '/SQL_Server_2019')]",
            "location": "northcentralus",
            "dependsOn": [
                "[resourceId('Microsoft.Sql/servers', parameters('servers_azuresql2019_name'))]"
integration.json
{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "deploymentId": {
integration.json
{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "deploymentId": {
integration.json
{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "deploymentId": {

Parameters

  • apiVersion required - string
  • location required - string

    Resource location.

  • name required - string

    The name of the database to be operated on (updated or created).

  • properties required
      • collation optional - string

        The collation of the database. If createMode is not Default, this value is ignored.

      • createMode optional - string

        Specifies the mode of database creation. Default: regular database creation. Copy: creates a database as a copy of an existing database. sourceDatabaseId must be specified as the resource ID of the source database. OnlineSecondary/NonReadableSecondary: creates a database as a (readable or nonreadable) secondary replica of an existing database. sourceDatabaseId must be specified as the resource ID of the existing primary database. PointInTimeRestore: Creates a database by restoring a point in time backup of an existing database. sourceDatabaseId must be specified as the resource ID of the existing database, and restorePointInTime must be specified. Recovery: Creates a database by restoring a geo-replicated backup. sourceDatabaseId must be specified as the recoverable database resource ID to restore. Restore: Creates a database by restoring a backup of a deleted database. sourceDatabaseId must be specified. If sourceDatabaseId is the database's original resource ID, then sourceDatabaseDeletionDate must be specified. Otherwise sourceDatabaseId must be the restorable dropped database resource ID and sourceDatabaseDeletionDate is ignored. restorePointInTime may also be specified to restore from an earlier point in time. RestoreLongTermRetentionBackup: Creates a database by restoring from a long term retention vault. recoveryServicesRecoveryPointResourceId must be specified as the recovery point resource ID. Copy, NonReadableSecondary, OnlineSecondary and RestoreLongTermRetentionBackup are not supported for DataWarehouse edition.

      • edition optional - string

        The edition of the database. The DatabaseEditions enumeration contains all the valid editions. If createMode is NonReadableSecondary or OnlineSecondary, this value is ignored.

        The list of SKUs may vary by region and support offer. To determine the SKUs (including the SKU name, tier/edition, family, and capacity) that are available to your subscription in an Azure region, use the Capabilities_ListByLocation REST API or one of the following commands:

        az sql db list-editions -l <location> -o table
        
        Get-AzSqlServerServiceObjective -Location <location>
        

        .

      • elasticPoolName optional - string

        The name of the elastic pool the database is in. If elasticPoolName and requestedServiceObjectiveName are both updated, the value of requestedServiceObjectiveName is ignored. Not supported for DataWarehouse edition.

      • maxSizeBytes optional - string

        The max size of the database expressed in bytes. If createMode is not Default, this value is ignored. To see possible values, query the capabilities API (/subscriptions/{subscriptionId}/providers/Microsoft.Sql/locations/{locationID}/capabilities) referred to by operationId: "Capabilities_ListByLocation."

      • readScale optional - string

        Conditional. If the database is a geo-secondary, readScale indicates whether read-only connections are allowed to this database or not. Not supported for DataWarehouse edition.

      • recoveryServicesRecoveryPointResourceId optional - string

        Conditional. If createMode is RestoreLongTermRetentionBackup, then this value is required. Specifies the resource ID of the recovery point to restore from.

      • requestedServiceObjectiveId optional - string

        The configured service level objective ID of the database. This is the service level objective that is in the process of being applied to the database. Once successfully updated, it will match the value of currentServiceObjectiveId property. If requestedServiceObjectiveId and requestedServiceObjectiveName are both updated, the value of requestedServiceObjectiveId overrides the value of requestedServiceObjectiveName.

        The list of SKUs may vary by region and support offer. To determine the service objective ids that are available to your subscription in an Azure region, use the Capabilities_ListByLocation REST API.

      • requestedServiceObjectiveName optional - string

        The name of the configured service level objective of the database. This is the service level objective that is in the process of being applied to the database. Once successfully updated, it will match the value of serviceLevelObjective property.

        The list of SKUs may vary by region and support offer. To determine the SKUs (including the SKU name, tier/edition, family, and capacity) that are available to your subscription in an Azure region, use the Capabilities_ListByLocation REST API or one of the following commands:

        az sql db list-editions -l <location> -o table
        
        Get-AzSqlServerServiceObjective -Location <location>
        

        .

      • restorePointInTime optional - string

        Conditional. If createMode is PointInTimeRestore, this value is required. If createMode is Restore, this value is optional. Specifies the point in time (ISO8601 format) of the source database that will be restored to create the new database. Must be greater than or equal to the source database's earliestRestoreDate value.

      • sampleName optional - string

        Indicates the name of the sample schema to apply when creating this database. If createMode is not Default, this value is ignored. Not supported for DataWarehouse edition.

      • sourceDatabaseDeletionDate optional - string

        Conditional. If createMode is Restore and sourceDatabaseId is the deleted database's original resource id when it existed (as opposed to its current restorable dropped database id), then this value is required. Specifies the time that the database was deleted.

      • sourceDatabaseId optional - string

        Conditional. If createMode is Copy, NonReadableSecondary, OnlineSecondary, PointInTimeRestore, Recovery, or Restore, then this value is required. Specifies the resource ID of the source database. If createMode is NonReadableSecondary or OnlineSecondary, the name of the source database must be the same as the new database being created.

      • zoneRedundant optional - boolean

        Whether or not this database is zone redundant, which means the replicas of this database will be spread across multiple availability zones.

  • tags optional - string

    Resource tags.

  • type required - string

Frequently asked questions

What is Azure Database SQL Server Database?

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

For Terraform, the Artspy/Terraform, gilyas/infracost and timwebster9/azure-policy source code examples are useful. See the Terraform Example section for further details.

For Azure Resource Manager, the elbromed/repo, emilefraser/PyroTermuxPackage and DeepthiJS/ProductAppSample source code examples are useful. See the Azure Resource Manager Example section for further details.