|
| 1 | +# Azure SQL Database Using Failover Groups with Private endpoints |
| 2 | + |
| 3 | +Terraform module for Azure to create a SQL server with initial database, Azure AD login, Firewall rules for SQL, Failover Group, Private endpoint, and corresponding private DNS zone for privatelink A records. It also allows creating an SQL server database with a SQL script initialization. |
| 4 | + |
| 5 | +## Configure the Azure Provider |
| 6 | + |
| 7 | +Add AzureRM provider to start with the module configuration. Whilst the `version` attribute is optional, we recommend, not to pinning to a given version of the Provider. |
| 8 | + |
| 9 | +## Create resource group |
| 10 | + |
| 11 | +By default, this module will not create a resource group and the name of an existing resource group to be given in an argument `create_resource_group`. If you want to create a new resource group, set the argument `create_resource_group = true`. |
| 12 | + |
| 13 | +*If you are using an existing resource group, then this module uses the same resource group location to create all resources in this module.* |
| 14 | + |
| 15 | +## Tagging |
| 16 | + |
| 17 | +Use tags to organize your Azure resources and management hierarchy. You can apply tags to your Azure resources, resource groups, and subscriptions to logically organize them into a taxonomy. Each tag consists of a name and a value pair. For example, you can apply the name "Environment" and the value "Production" to all the resources in production. You can manage these values variables directly or mapping as a variable using `variables.tf`. |
| 18 | + |
| 19 | +All Azure resources which support tagging can be tagged by specifying key-values in argument `tags`. Tag Name is added automatically on all resources. For example, you can specify `tags` like this: |
| 20 | + |
| 21 | +``` |
| 22 | +module "mssql-server" { |
| 23 | + source = "github.com/kumarvit/terraform-azurerm-mssql-db" |
| 24 | + create_resource_group = false |
| 25 | +
|
| 26 | + # ... omitted |
| 27 | +
|
| 28 | + tags = { |
| 29 | + Terraform = "true" |
| 30 | + Environment = "dev" |
| 31 | + Owner = "test-user" |
| 32 | + } |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +## Create schema and Initialize SQL Database |
| 37 | + |
| 38 | +This module uses the tool slqcmd as a local provisioner to connect and inject the SQL initialization. To enable this feature set the argument `initialize_sql_script_execution = true` and use `sqldb_init_script_file` argument to provide the path to SQL script. |
| 39 | + |
| 40 | +> #### *Note: To run this utility from your desktop, to create SQL database schema using SQL script requires firewall rule. Allow access to Azure services can be enabled by setting `start_ip_address` and `end_ip_address` to `0.0.0.0` and add your machine public IP to SQL firewall rules to run this feature else this will fail to run and exit the terraform plan.* |
| 41 | +
|
| 42 | +> #### *Note: Enabling `extended_auditing_policy` and `threat_detection_policy` features on SQL servers and database going to create a storage account to keep all audit logs. Log retention policy to be configured to keep the size within limits for this storage account. Note that this module creates resources that can cost money.* |
| 43 | +
|
| 44 | +> #### *Note: If you prefer private endpoints feature, firewall rules are not relevant. However, this module can support both the Public and Private availability of the Database. Disable the firewall rules, in case you want to create the database using private endpoints only.* |
| 45 | +
|
| 46 | +## Module Usage |
| 47 | + |
| 48 | +### Simple Azure SQL single database creation |
| 49 | + |
| 50 | +Following example is to create a simple database with basic firewall rules to make SQL database available to Azure resources, services and client IP ranges. This module also supports optional AD admin user for DB, Audit Polices, and creation of database schema using SQL script. |
| 51 | + |
| 52 | +``` |
| 53 | +module "mssql-server" { |
| 54 | + source = "github.com/kumarvit/terraform-azurerm-mssql-db" |
| 55 | +
|
| 56 | +# Resource Group, VNet and Subnet declarations |
| 57 | + create_resource_group = false |
| 58 | + resource_group_name = "rg-demo-westeurope-01" |
| 59 | + location = "westeurope" |
| 60 | + virtual_network_name = "vnet-demo-westeurope-001" |
| 61 | + private_subnet_address_prefix = "10.0.5.0/29" |
| 62 | +
|
| 63 | +# SQL Server and Database scaling options |
| 64 | + sqlserver_name = "sqldbserver-db01" |
| 65 | + database_name = "demomssqldb" |
| 66 | + sql_database_edition = "Standard" |
| 67 | + sqldb_service_objective_name = "S1" |
| 68 | +
|
| 69 | +# SQL Server and Database Audit policies |
| 70 | + enable_auditing_policy = true |
| 71 | + enable_threat_detection_policy = true |
| 72 | + log_retention_days = 30 |
| 73 | + email_addresses_for_alerts = ["[email protected]"] |
| 74 | +
|
| 75 | +# AD administrator for an Azure SQL server |
| 76 | + enable_sql_ad_admin = true |
| 77 | + ad_admin_login_name = "[email protected]" |
| 78 | +
|
| 79 | +# Firewall Rules to allow azure and external clients |
| 80 | + enable_firewall_rules = true |
| 81 | + firewall_rules = [ |
| 82 | + {name = "access-to-azure" |
| 83 | + start_ip_address = "0.0.0.0" |
| 84 | + end_ip_address = "0.0.0.0"}, |
| 85 | + {name = "desktop-ip" |
| 86 | + start_ip_address = "123.201.75.71" |
| 87 | + end_ip_address = "123.201.75.71"}] |
| 88 | +
|
| 89 | +# Create and initialize a database with SQL script |
| 90 | + initialize_sql_script_execution = false |
| 91 | + sqldb_init_script_file = "./artifacts/db-init-sample.sql" |
| 92 | +
|
| 93 | +# Tags for Azure Resources |
| 94 | + tags = { |
| 95 | + Terraform = "true" |
| 96 | + Environment = "dev" |
| 97 | + Owner = "test-user" |
| 98 | + } |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +### Simple Azure SQL single database using private Endpoint |
| 103 | + |
| 104 | +Following example to create a SQL single database using private endpoints. This module also supports optional AD admin user for DB, Audit Policies, and creation of database schema using SQL script. |
| 105 | + |
| 106 | +``` |
| 107 | +module "mssql-server" { |
| 108 | + source = "github.com/kumarvit/terraform-azurerm-mssql-db" |
| 109 | +
|
| 110 | +# Resource Group, VNet and Subnet declarations |
| 111 | + create_resource_group = false |
| 112 | + resource_group_name = "rg-demo-westeurope-01" |
| 113 | + location = "westeurope" |
| 114 | + virtual_network_name = "vnet-demo-westeurope-001" |
| 115 | + private_subnet_address_prefix = "10.0.5.0/29" |
| 116 | +
|
| 117 | +# SQL Server and Database scaling options |
| 118 | + sqlserver_name = "sqldbserver-db01" |
| 119 | + database_name = "demomssqldb" |
| 120 | + sql_database_edition = "Standard" |
| 121 | + sqldb_service_objective_name = "S1" |
| 122 | +
|
| 123 | +# SQL Server and Database Audit policies |
| 124 | + enable_auditing_policy = true |
| 125 | + enable_threat_detection_policy = true |
| 126 | + log_retention_days = 30 |
| 127 | + email_addresses_for_alerts = ["[email protected]"] |
| 128 | +
|
| 129 | +# AD administrator for an Azure SQL server |
| 130 | + enable_sql_ad_admin = true |
| 131 | + ad_admin_login_name = "[email protected]" |
| 132 | +
|
| 133 | +# Private Endpoint for Sql servers |
| 134 | + enable_private_endpoint = true |
| 135 | +
|
| 136 | +# Create and initialize a database with SQL script |
| 137 | + initialize_sql_script_execution = false |
| 138 | + sqldb_init_script_file = "./artifacts/db-init-sample.sql" |
| 139 | +
|
| 140 | +# Tags for Azure Resources |
| 141 | + tags = { |
| 142 | + Terraform = "true" |
| 143 | + Environment = "dev" |
| 144 | + Owner = "test-user" |
| 145 | + } |
| 146 | +} |
| 147 | +``` |
| 148 | + |
| 149 | +### Azure SQL database creation using geo-replication with auto-failover groups |
| 150 | + |
| 151 | +Following example to create a SQL database using geo-replication with auto-failover groups. This module also supports optional AD admin user for DB, Audit Policies, Firewall Rules, and creation of database schema using SQL script. |
| 152 | + |
| 153 | +``` |
| 154 | +module "mssql-server" { |
| 155 | + source = "github.com/kumarvit/terraform-azurerm-mssql-db" |
| 156 | +
|
| 157 | +# Resource Group, VNet and Subnet declarations |
| 158 | + create_resource_group = false |
| 159 | + resource_group_name = "rg-demo-westeurope-01" |
| 160 | + location = "westeurope" |
| 161 | + virtual_network_name = "vnet-demo-westeurope-001" |
| 162 | + private_subnet_address_prefix = "10.0.5.0/29" |
| 163 | +
|
| 164 | +# SQL Server and Database scaling options |
| 165 | + sqlserver_name = "sqldbserver-db01" |
| 166 | + database_name = "demomssqldb" |
| 167 | + sql_database_edition = "Standard" |
| 168 | + sqldb_service_objective_name = "S1" |
| 169 | +
|
| 170 | +# SQL Server and Database Audit policies |
| 171 | + enable_auditing_policy = true |
| 172 | + enable_threat_detection_policy = true |
| 173 | + log_retention_days = 30 |
| 174 | + email_addresses_for_alerts = ["[email protected]"] |
| 175 | +
|
| 176 | +# AD administrator for an Azure SQL server |
| 177 | + enable_sql_ad_admin = true |
| 178 | + ad_admin_login_name = "[email protected]" |
| 179 | +
|
| 180 | +# Firewall Rules to allow azure and external clients |
| 181 | + enable_firewall_rules = true |
| 182 | + firewall_rules = [ |
| 183 | + {name = "access-to-azure" |
| 184 | + start_ip_address = "0.0.0.0" |
| 185 | + end_ip_address = "0.0.0.0"}, |
| 186 | + {name = "desktop-ip" |
| 187 | + start_ip_address = "123.201.75.71" |
| 188 | + end_ip_address = "123.201.75.71"}] |
| 189 | +
|
| 190 | +# Sql failover group |
| 191 | + enable_failover_group = true |
| 192 | + secondary_sql_server_location = "northeurope" |
| 193 | +
|
| 194 | +# Create and initialize a database with SQL script |
| 195 | + initialize_sql_script_execution = false |
| 196 | + sqldb_init_script_file = "./artifacts/db-init-sample.sql" |
| 197 | +
|
| 198 | +# Tags for Azure Resources |
| 199 | + tags = { |
| 200 | + Terraform = "true" |
| 201 | + Environment = "dev" |
| 202 | + Owner = "test-user" |
| 203 | + } |
| 204 | +} |
| 205 | +``` |
| 206 | + |
| 207 | +### Azure SQL database creation using geo-replication with auto-failover groups and Private Endpoints |
| 208 | + |
| 209 | +Following example to create a SQL database using geo-replication with auto-failover groups and private endpoints. This module also supports optional AD admin user for DB, Audit Policies, Firewall Rules, and creation of database schema using SQL script. |
| 210 | + |
| 211 | +``` |
| 212 | +module "mssql-server" { |
| 213 | + source = "github.com/kumarvit/terraform-azurerm-mssql-db" |
| 214 | +
|
| 215 | +# Resource Group, VNet and Subnet declarations |
| 216 | + create_resource_group = false |
| 217 | + resource_group_name = "rg-demo-westeurope-01" |
| 218 | + location = "westeurope" |
| 219 | + virtual_network_name = "vnet-demo-westeurope-001" |
| 220 | + private_subnet_address_prefix = "10.0.5.0/29" |
| 221 | +
|
| 222 | +# SQL Server and Database scaling options |
| 223 | + sqlserver_name = "sqldbserver-db01" |
| 224 | + database_name = "demomssqldb" |
| 225 | + sql_database_edition = "Standard" |
| 226 | + sqldb_service_objective_name = "S1" |
| 227 | +
|
| 228 | +# SQL Server and Database Audit policies |
| 229 | + enable_auditing_policy = true |
| 230 | + enable_threat_detection_policy = true |
| 231 | + log_retention_days = 30 |
| 232 | + email_addresses_for_alerts = ["[email protected]"] |
| 233 | +
|
| 234 | +# AD administrator for an Azure SQL server |
| 235 | + enable_sql_ad_admin = true |
| 236 | + ad_admin_login_name = "[email protected]" |
| 237 | +
|
| 238 | +# Sql failover group |
| 239 | + enable_failover_group = true |
| 240 | + secondary_sql_server_location = "northeurope" |
| 241 | +
|
| 242 | +# Private Endpoint for Sql servers |
| 243 | + enable_private_endpoint = true |
| 244 | +
|
| 245 | +# Create and initialize a database with SQL script |
| 246 | + initialize_sql_script_execution = false |
| 247 | + sqldb_init_script_file = "./artifacts/db-init-sample.sql" |
| 248 | +
|
| 249 | +# Tags for Azure Resources |
| 250 | + tags = { |
| 251 | + Terraform = "true" |
| 252 | + Environment = "dev" |
| 253 | + Owner = "test-user" |
| 254 | + } |
| 255 | +} |
| 256 | +``` |
| 257 | + |
| 258 | +## Terraform Usage |
| 259 | + |
| 260 | +To run this example you need to execute following Terraform commands |
| 261 | + |
| 262 | +``` |
| 263 | +$ terraform init |
| 264 | +$ terraform plan |
| 265 | +$ terraform apply |
| 266 | +``` |
| 267 | + |
| 268 | +Run `terraform destroy` when you don't need these resources. |
| 269 | + |
| 270 | +## Outputs |
| 271 | + |
| 272 | +Name | Description |
| 273 | +---- | ----------- |
| 274 | +`resource_group_name` | The name of the resource group in which resources are created |
| 275 | +`resource_group_location`| The location of the resource group in which resources are created |
| 276 | +`storage_account_id`|The ID of the storage account |
| 277 | +`storage_account_name`|The name of the storage account |
| 278 | +`primary_sql_server_id`|The primary Microsoft SQL Server ID |
| 279 | +`primary_sql_server_fqdn`|The fully qualified domain name of the primary Azure SQL Server |
| 280 | +`secondary_sql_server_id`|The secondary Microsoft SQL Server ID |
| 281 | +`secondary_sql_server_fqdn`|The fully qualified domain name of the secondary Azure SQL Server |
| 282 | +`sql_server_admin_user`|SQL database administrator login id |
| 283 | +`sql_server_admin_password`|SQL database administrator login password |
| 284 | +`sql_database_id`|The SQL Database ID |
| 285 | +`sql_database_name`|The SQL Database Name |
| 286 | +`sql_failover_group_id`|A failover group of databases on a collection of Azure SQL servers |
| 287 | +`primary_sql_server_private_endpoint`|id of the Primary SQL server Private Endpoint |
| 288 | +`secondary_sql_server_private_endpoint`|id of the Primary SQL server Private Endpoint |
| 289 | +`sql_server_private_dns_zone_domain`|DNS zone name of SQL server Private endpoints dns name records |
| 290 | +`primary_sql_server_private_endpoint_ip`|Priamary SQL server private endpoint IPv4 Addresses |
| 291 | +`primary_sql_server_private_endpoint_fqdn`|Priamary SQL server private endpoint IPv4 Addresses |
| 292 | +`secondary_sql_server_private_endpoint_ip`|Secondary SQL server private endpoint IPv4 Addresses |
| 293 | +`secondary_sql_server_private_endpoint_fqdn`|Secondary SQL server private endpoint IPv4 Addresses |
0 commit comments