Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MS SQL Database module #58

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
38 changes: 38 additions & 0 deletions modules/mssql-database/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 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).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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).
This module creates an [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_database" {
source = "https://github.com/gofrontier-com/azurerm-terraform-modules/releases/download/mssql-database/[VERSION]/module.tar.gz//src"

environment = "dev"
identifier = "mortgages"
location = "uksouth"
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-${var.zone}-${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
Comment on lines +3 to +9
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we match the var names?


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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
email_addresses = var.threat_detection_policy.security_alert_email_addresses
email_addresses = var.threat_detection_policy.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
}
Comment on lines +24 to +27
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a sensible default we can add, like with other modules?

type = list(string)
default = []
}

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

variable "metric_categories" {
type = list(string)
default = []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
default = []
default = [
"AllMetrics",
]

}

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)."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set a serverless as default?

}

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" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
module "mssql-server" {
module "mssql_database" {

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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file in src, too?

required_version = "~> 1.5"

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