Skip to content

Commit

Permalink
Add MS SQL Database module
Browse files Browse the repository at this point in the history
  • Loading branch information
CDA0 committed Jan 20, 2024
1 parent 9361cfa commit d615417
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 0 deletions.
1 change: 1 addition & 0 deletions modules/mssql-database/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Changelog
39 changes: 39 additions & 0 deletions modules/mssql-database/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# MS SQL Database

This module creates a [MS SQL Database](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/mssql_database) and associated [Diagnostic Setting](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/monitor_diagnostic_setting).

## Usage

```hcl
module "mssql_server" {
source = "https://github.com/gofrontier-com/azurerm-terraform-modules/releases/download/mssql-database/[VERSION]/module.tar.gz//src"
environment = "dev"
identifier = "mortgages"
location = "uksouth"
resource_group_name = module.resource_group.name
zone = "mtg"
threat_detection_policy = {
retention_days = 10
email_account_admins = true
security_alert_email_addresses = ["[email protected]"]
storage_account_access_key = "abc123"
storage_endpoint = "endpoint"
}
log_analytics_workspace_id = data.azurerm_log_analytics_workspace.main.id
tags = {
WorkloadType = "MortgagesLZ/data-platform"
}
}
```

## Known issues

_None._

## Contributing

See <https://github.com/gofrontier-com/azurerm-terraform-modules/blob/main/README.rst#contributing>.
1 change: 1 addition & 0 deletions modules/mssql-database/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.0
14 changes: 14 additions & 0 deletions modules/mssql-database/src/locals.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
locals {
identifier = replace(lower(var.identifier), "/[^a-z1-9]/", "")

short_locations = {
"uksouth" = "uks"
"ukwest" = "ukw"
}

tags = {
Environment = var.environment
Location = var.location
Zone = var.zone
}
}
59 changes: 59 additions & 0 deletions modules/mssql-database/src/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
resource "azurerm_mssql_database" "main" {
name = "sdb-ct-${var.environment}-${lookup(local.short_locations, var.location)}-${local.identifier}"
server_id = var.sql_server_id
max_size_gb = var.max_size_gb
sku_name = var.sql_database_sku
collation = var.sql_database_collation

min_capacity = var.serverless_min_capacity
auto_pause_delay_in_minutes = var.serverless_auto_pause_delay

dynamic "threat_detection_policy" {
for_each = var.threat_detection_policy != null ? [{}] : []
content {
state = "Enabled"
retention_days = var.threat_detection_policy.retention_days
email_account_admins = var.threat_detection_policy.email_account_admins
email_addresses = var.threat_detection_policy.security_alert_email_addresses
storage_account_access_key = var.threat_detection_policy.storage_account_access_key
storage_endpoint = var.threat_detection_policy.storage_endpoint
}
}

tags = merge(var.tags, local.tags)

lifecycle {
prevent_destroy = true
}
}

resource "azurerm_monitor_diagnostic_setting" "main" {
name = "log-analytics"
target_resource_id = azurerm_mssql_database.main.id
log_analytics_workspace_id = var.log_analytics_workspace_id
log_analytics_destination_type = "AzureDiagnostics"

dynamic "enabled_log" {
for_each = var.log_categories

content {
category = enabled_log.value
}
}

dynamic "enabled_log" {
for_each = var.log_category_groups

content {
category_group = enabled_log.value
}
}

dynamic "metric" {
for_each = var.metric_categories

content {
category = metric.value
}
}
}
3 changes: 3 additions & 0 deletions modules/mssql-database/src/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "id" {
value = azurerm_mssql_database.main.id
}
91 changes: 91 additions & 0 deletions modules/mssql-database/src/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
variable "environment" {
type = string
}

variable "identifier" {
type = string
}

variable "location" {
type = string
}

variable "log_analytics_workspace_id" {
type = string
}

variable "log_categories" {
type = list(string)
default = []
}

variable "log_category_groups" {
type = list(string)
default = []
}

variable "max_size_gb" {
type = number
default = 32
}

variable "metric_categories" {
type = list(string)
default = []
}

variable "retention_days" {
type = string
default = "30"
}

variable "security_alert_email_addresses" {
type = list(string)
default = []
}

variable "serverless_min_capacity" {
type = string
default = null
description = "Minimum capacity for serverless SKUs. Minimum value is 0.5 (vCore)."
}

variable "serverless_auto_pause_delay" {
type = string
default = null
description = "The auto-pause delay for serverless SKUs. Minimum value is 60 (minutes)."
}

variable "sql_database_collation" {
type = string
default = "SQL_LATIN1_GENERAL_CP1_CI_AS"
}

variable "sql_database_sku" {
type = string
default = "GP_Gen5_2"
}

variable "sql_server_id" {
type = string
}

variable "tags" {
type = map(string)
default = {}
}

variable "threat_detection_policy" {
type = object({
retention_days = number
email_account_admins = bool
security_alert_email_addresses = list(string)
storage_account_access_key = string
storage_endpoint = string
})
default = null
}

variable "zone" {
type = string
}
19 changes: 19 additions & 0 deletions modules/mssql-database/test/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
provider "azurerm" {
features {}
}

module "mssql-server" {
source = "../src"

environment = "baz"
identifier = "qux"
location = "uksouth"
zone = "waldo"

log_analytics_workspace_id = "quux"
sql_server_id = "123"

tags = {
Foo = "Bar"
}
}
10 changes: 10 additions & 0 deletions modules/mssql-database/test/terraform.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
terraform {
required_version = "~> 1.5"

required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.85"
}
}
}

0 comments on commit d615417

Please sign in to comment.