Skip to content

Commit c295b89

Browse files
authored
Merge pull request #1 from data-platform-hq/add-module
feat: add mssql database module
2 parents 376685d + 0a731d5 commit c295b89

File tree

5 files changed

+193
-3
lines changed

5 files changed

+193
-3
lines changed

README.md

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,61 @@
1-
# Azure <> Terraform module
2-
Terraform module for creation Azure <>
1+
# Azure Microsoft SQL Database Server Terraform module
2+
Terraform module for creation Azure Microsoft SQL Database Server
33

44
## Usage
55

66
<!-- BEGIN_TF_DOCS -->
7+
## Requirements
78

9+
| Name | Version |
10+
| ------------------------------------------------------------------------- | --------- |
11+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0.0 |
12+
| <a name="requirement_azurerm"></a> [azurerm](#requirement\_azurerm) | >= 3.23.0 |
13+
14+
## Providers
15+
16+
| Name | Version |
17+
| ------------------------------------------------------------- | ------- |
18+
| <a name="provider_azurerm"></a> [azurerm](#provider\_azurerm) | 3.24.0 |
19+
20+
## Modules
21+
22+
No modules.
23+
24+
## Resources
25+
26+
| Name | Type |
27+
| ----------------------------------------------------------------------------------------------------------------------------- | -------- |
28+
| [azurerm_mssql_database.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/mssql_database) | resource |
29+
30+
## Inputs
31+
32+
| Name | Description | Type | Default | Required |
33+
| --------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------ | ------------------ | ---------------------------- | :------: |
34+
| <a name="input_server_id"></a> [server\_id](#input\_server\_id) | Id of SQL server | `string` | n/a | yes |
35+
| <a name="input_server_fqdn"></a> [server\_fqdn](#input\_server\_fqdn) | FQDN of Azure SQL Server | `string` | n/a | yes |
36+
| <a name="input_tags"></a> [tags](#input\_tags) | tags for resources | `map(string)` | {} | no |
37+
| <a name="input_default_collation"></a> [default\_collation](#input\_default\_collation) | Specifies the collation of the database | `string` | SQL_Latin1_General_CP1_CI_AS | no |
38+
| <a name="input_default_sku"></a> [default\_sku](#input\_default\_sku) | Specifies the SKU of the database | `string` | GP_S_Gen5_1 | no |
39+
| <a name="input_default_max_size"></a> [default\_max_size](#input\_default\_max\_size) | The max size of the database in gigabytes | `string` | 20 | no |
40+
| <a name="input_default_min_capacity"></a> [default\_min\_capacity](#input\_default\_min\_capacity) | The min size of the database in gigabytes | `string` | 0.5 | no |
41+
| <a name="input_default_autopause_delay"></a> [default\_autopause\_delay](#input\_default\_autopause\_delay) | Time in minutes after which database is automatically paused. A value of -1 means that automatic pause is disabled | `string` | 60 | no |
42+
| <a name="input_default_retention_days"></a> [default\_retention\_days](#input\_default\_retention\_days) | Specifies the number of days to keep in the Threat Detection audit logs | `string` | 3 | no |
43+
| <a name="input_default_create_mode"></a> [default\_create\_mode](#input\_default\_create\_mode) | Type of create mode selected in database config object | `string` | Default | no |
44+
| <a name="input_default_creation_source_database_id"></a> [default\_creation\_source\_database\_id](#input\_default\_creation\_source\_database\_id) | This variable is used in case 'create_mode'='Copy' | `string` | null | no |
45+
| <a name="input_storage_account_type"></a> [storage\_account\_type](#input\_storage\_account\_type) | Specifies the storage account type used to store backups for this database | `string` | ZRS | no |
46+
| <a name="input_databases"></a> [databases](#input\_databases) | "Map of databases | `map(map(string))` | {} | no |
47+
48+
## Outputs
49+
50+
| Name | Description |
51+
| ---------------------------------------------------------------------------------------------------------- | --------------------------------------------------------- |
52+
| <a name="output_mssql_database_secrets"></a> [mssql\_database\_secrets](#output\_mssql\_database\_secrets) | Map of Database Name to JDBC Connection String |
53+
| <a name="output_sql_server_id"></a> [sql\_server\_id](#output\_sql\_server\_id) | Id of SQL server |
54+
| <a name="output_sql_database_names"></a> [sql\_database\_names](#output\_sql\_database\_names) | Database name of the Azure SQL Database created |
55+
| <a name="output_sql_database_max_size"></a> [sql\_database\_max\_size](#output\_sql\_database\_max\_size) | Database max size in GB of the Azure SQL Database created |
56+
| <a name="output_storage_account_type"></a> [storage\_account\_type](#output\_storage\_account\_type) | Storage Account Type |
857
<!-- END_TF_DOCS -->
958

1059
## License
1160

12-
Apache 2 Licensed. For more information please see [LICENSE](https://github.com/data-platform-hq/terraform-azurerm<>/tree/master/LICENSE)
61+
Apache 2 Licensed. For more information please see [LICENSE](https://github.com/data-platform-hq/terraform-azurerm-mssql-database/blob/main/LICENSE)

main.tf

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
resource "azurerm_mssql_database" "this" {
2+
for_each = var.databases
3+
4+
name = each.key
5+
server_id = var.server_id
6+
collation = lookup(each.value, "collation", var.default_collation)
7+
sku_name = lookup(each.value, "sku", var.default_sku)
8+
max_size_gb = lookup(each.value, "max_size", var.default_max_size)
9+
min_capacity = lookup(each.value, "min_capacity", var.default_min_capacity)
10+
auto_pause_delay_in_minutes = lookup(each.value, "auto_pause_delay", var.default_autopause_delay)
11+
create_mode = lookup(each.value, "create_mode", var.default_create_mode)
12+
creation_source_database_id = lookup(each.value, "creation_source_database_id", var.default_creation_source_database_id)
13+
storage_account_type = var.storage_account_type == "ZRS" ? "Zone" : "Geo"
14+
tags = var.tags
15+
16+
short_term_retention_policy {
17+
retention_days = lookup(each.value, "retention_days", var.default_retention_days)
18+
}
19+
20+
lifecycle {
21+
ignore_changes = [
22+
sku_name,
23+
]
24+
}
25+
}

outputs.tf

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
locals {
2+
dbname_list = [for n in azurerm_mssql_database.this : n.name]
3+
secret_map = { for name in local.dbname_list : "mssql-${lower(name)}-database-url" => {
4+
value = "jdbc:sqlserver://${var.server_fqdn}:1433;database=${name}"
5+
} }
6+
}
7+
8+
output "mssql_database_secrets" {
9+
value = local.secret_map
10+
description = "Map of Database Name to JDBC Connection String"
11+
}
12+
13+
output "sql_server_id" {
14+
value = { for k, v in azurerm_mssql_database.this : k => v.server_id }
15+
description = "Id of SQL server"
16+
}
17+
18+
output "sql_database_names" {
19+
value = { for k, v in azurerm_mssql_database.this : k => v.name }
20+
description = "Database name of the Azure SQL Database created."
21+
}
22+
23+
output "sql_database_max_size" {
24+
value = { for k, v in azurerm_mssql_database.this : k => v.max_size_gb }
25+
description = "Database max size in GB of the Azure SQL Database created."
26+
}
27+
28+
output "storage_account_type" {
29+
value = { for k, v in azurerm_mssql_database.this : k => v.storage_account_type }
30+
description = "Storage Account Type"
31+
}

variables.tf

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
variable "server_id" {
2+
type = string
3+
description = "Id of SQL server"
4+
}
5+
6+
variable "server_fqdn" {
7+
type = string
8+
description = "FQDN of Azure SQL Server"
9+
}
10+
11+
variable "tags" {
12+
type = map(string)
13+
description = "tags for resources"
14+
default = {}
15+
}
16+
17+
variable "default_collation" {
18+
type = string
19+
description = "Specifies the collation of the database"
20+
default = "SQL_Latin1_General_CP1_CI_AS"
21+
}
22+
23+
variable "default_sku" {
24+
type = string
25+
description = "Specifies the SKU of the database"
26+
default = "GP_S_Gen5_1"
27+
}
28+
29+
variable "default_max_size" {
30+
type = string
31+
description = "The max size of the database in gigabytes"
32+
default = "20"
33+
}
34+
35+
variable "default_min_capacity" {
36+
type = string
37+
description = "The min size of the database in gigabytes"
38+
default = "0.5"
39+
}
40+
41+
variable "default_autopause_delay" {
42+
type = number
43+
description = "Time in minutes after which database is automatically paused. A value of -1 means that automatic pause is disabled"
44+
default = 60
45+
}
46+
47+
variable "default_retention_days" {
48+
type = number
49+
description = "Specifies the number of days to keep in the Threat Detection audit logs."
50+
default = 3
51+
}
52+
53+
variable "default_create_mode" {
54+
type = string
55+
description = "Type of create mode selected in database config object"
56+
default = "Default"
57+
}
58+
59+
variable "default_creation_source_database_id" {
60+
type = string
61+
description = "This variable is used in case 'create_mode'='Copy'"
62+
default = null
63+
}
64+
65+
variable "storage_account_type" {
66+
type = string
67+
description = "Specifies the storage account type used to store backups for this database"
68+
default = "ZRS"
69+
}
70+
71+
variable "databases" {
72+
type = map(map(string))
73+
description = "Map of databases"
74+
default = {}
75+
}

versions.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
required_version = ">= 1.0.0"
3+
4+
required_providers {
5+
azurerm = {
6+
source = "hashicorp/azurerm"
7+
version = ">= 3.23.0"
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)