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_minutes
optional computed - number -
collation
optional computed - string -
create_mode
optional computed - string -
creation_source_database_id
optional computed - string -
elastic_pool_id
optional - string -
extended_auditing_policy
optional 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_enabled
optional - bool -
id
optional computed - string -
license_type
optional computed - string -
max_size_gb
optional computed - number -
min_capacity
optional computed - number -
name
required - string -
read_replica_count
optional computed - number -
read_scale
optional computed - bool -
recover_database_id
optional - string -
restore_dropped_database_id
optional - string -
restore_point_in_time
optional computed - string -
sample_name
optional computed - string -
server_id
required - string -
sku_name
optional computed - string -
storage_account_type
optional - string -
tags
optional - map from string to string -
zone_redundant
optional computed - bool -
long_term_retention_policy
list block-
monthly_retention
optional computed - string -
week_of_year
optional computed - number -
weekly_retention
optional computed - string -
yearly_retention
optional computed - string
-
-
short_term_retention_policy
list block-
retention_days
required - number
-
-
threat_detection_policy
list block-
disabled_alerts
optional - set of string -
email_account_admins
optional - string -
email_addresses
optional - set of string -
retention_days
optional - number -
state
optional - string -
storage_account_access_key
optional - string -
storage_endpoint
optional - string -
use_server_default
optional - string
-
-
timeouts
single 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
apiVersion
required - stringlocation
required - stringResource location.
name
required - stringThe name of the database to be operated on (updated or created).
properties
requiredcollation
optional - stringThe collation of the database. If createMode is not Default, this value is ignored.
createMode
optional - 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.
edition
optional - 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_ListByLocation
REST API or one of the following commands:az sql db list-editions -l <location> -o table
Get-AzSqlServerServiceObjective -Location <location>
.
elasticPoolName
optional - 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.
maxSizeBytes
optional - 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."
readScale
optional - 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.
recoveryServicesRecoveryPointResourceId
optional - stringConditional. If createMode is RestoreLongTermRetentionBackup, then this value is required. Specifies the resource ID of the recovery point to restore from.
requestedServiceObjectiveId
optional - 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_ListByLocation
REST API.requestedServiceObjectiveName
optional - 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_ListByLocation
REST API or one of the following commands:az sql db list-editions -l <location> -o table
Get-AzSqlServerServiceObjective -Location <location>
.
restorePointInTime
optional - 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.
sampleName
optional - 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.
sourceDatabaseDeletionDate
optional - 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.
sourceDatabaseId
optional - 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.
zoneRedundant
optional - booleanWhether or not this database is zone redundant, which means the replicas of this database will be spread across multiple availability zones.
tags
optional - stringResource 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.