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
resource "azurerm_mssql_database" "fitverse_ms_db" {
name = "-"
server_id = azurerm_sql_server.fitverse-sql-server.id
sku_name = "S0"
extended_auditing_policy {
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"
}
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
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
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"
}
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
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
resource "azurerm_mssql_database" "lab01" {
name = "lab01-db"
server_id = azurerm_mssql_server.lab01.id
}
resource "azurerm_mssql_database" "lab02" {
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
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
Parameters
-
auto_pause_delay_in_minutesoptional computed - number -
collationoptional computed - string -
create_modeoptional computed - string -
creation_source_database_idoptional computed - string -
elastic_pool_idoptional - string -
extended_auditing_policyoptional computed - list of object-
log_monitoring_enabled- bool -
retention_in_days- number -
storage_account_access_key- string -
storage_account_access_key_is_secondary- bool -
storage_endpoint- string
-
-
geo_backup_enabledoptional - bool -
idoptional computed - string -
license_typeoptional computed - string -
max_size_gboptional computed - number -
min_capacityoptional computed - number -
namerequired - string -
read_replica_countoptional computed - number -
read_scaleoptional computed - bool -
recover_database_idoptional - string -
restore_dropped_database_idoptional - string -
restore_point_in_timeoptional computed - string -
sample_nameoptional computed - string -
server_idrequired - string -
sku_nameoptional computed - string -
storage_account_typeoptional - string -
tagsoptional - map from string to string -
zone_redundantoptional computed - bool -
long_term_retention_policylist block-
monthly_retentionoptional computed - string -
week_of_yearoptional computed - number -
weekly_retentionoptional computed - string -
yearly_retentionoptional computed - string
-
-
short_term_retention_policylist block-
retention_daysrequired - number
-
-
threat_detection_policylist block-
disabled_alertsoptional - set of string -
email_account_adminsoptional - string -
email_addressesoptional - set of string -
retention_daysoptional - number -
stateoptional - string -
storage_account_access_keyoptional - string -
storage_endpointoptional - string -
use_server_defaultoptional - string
-
-
timeoutssingle block
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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
"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'))]"
"type": "Microsoft.Sql/servers/databases",
"apiVersion": "2019-06-01-preview",
"name": "[concat(parameters('servers_platformsupport01_name'), '/OrchestrationSupport')]",
"location": "uksouth",
"sku": {
"name": "Standard",
"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"
"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",
"type": "Microsoft.Sql/servers/databases",
"apiVersion": "2019-06-01-preview",
"name": "[concat(parameters('servers_platformsupport01_name'), '/OrchestrationSupport')]",
"location": "uksouth",
"sku": {
"name": "Standard",
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"deploymentId": {
"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'))]"
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"deploymentId": {
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"deploymentId": {
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"deploymentId": {
Parameters
apiVersionrequired - stringlocationrequired - stringResource location.
namerequired - stringThe name of the database to be operated on (updated or created).
propertiesrequiredcollationoptional - stringThe collation of the database. If createMode is not Default, this value is ignored.
createModeoptional - stringSpecifies 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.
editionoptional - stringThe 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_ListByLocationREST API or one of the following commands:az sql db list-editions -l <location> -o tableGet-AzSqlServerServiceObjective -Location <location>.
elasticPoolNameoptional - stringThe 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.
maxSizeBytesoptional - stringThe 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."
readScaleoptional - stringConditional. 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.
recoveryServicesRecoveryPointResourceIdoptional - stringConditional. If createMode is RestoreLongTermRetentionBackup, then this value is required. Specifies the resource ID of the recovery point to restore from.
requestedServiceObjectiveIdoptional - stringThe 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_ListByLocationREST API.requestedServiceObjectiveNameoptional - stringThe 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_ListByLocationREST API or one of the following commands:az sql db list-editions -l <location> -o tableGet-AzSqlServerServiceObjective -Location <location>.
restorePointInTimeoptional - stringConditional. 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.
sampleNameoptional - stringIndicates 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.
sourceDatabaseDeletionDateoptional - stringConditional. 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.
sourceDatabaseIdoptional - stringConditional. 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.
zoneRedundantoptional - booleanWhether or not this database is zone redundant, which means the replicas of this database will be spread across multiple availability zones.
tagsoptional - stringResource tags.
typerequired - 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.