Skip to content

Commit bc83fe0

Browse files
committed
feat: add mssql database module
1 parent 376685d commit bc83fe0

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

main.tf

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
15+
short_term_retention_policy {
16+
retention_days = lookup(each.value, "retention_days", var.default_retention_days)
17+
}
18+
19+
lifecycle {
20+
ignore_changes = [
21+
sku_name,
22+
]
23+
}
24+
}

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

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)