-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Changelog |
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). | ||
|
||
## 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>. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1.0 |
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 | ||
} | ||
} |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
} | ||||||||||
|
||||||||||
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" | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
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 | ||||||||||
} | ||||||||||
} | ||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
output "id" { | ||
value = azurerm_mssql_database.main.id | ||
} |
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" { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = [] | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
} | ||||||||||
|
||||||||||
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)." | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
} | ||||||||||
|
||||||||||
variable "serverless_auto_pause_delay" { | ||||||||||
type = string | ||||||||||
default = null | ||||||||||
description = "The auto-pause delay for serverless SKUs. Minimum value is 60 (minutes)." | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
} | ||||||||||
|
||||||||||
variable "sql_database_collation" { | ||||||||||
type = string | ||||||||||
default = "SQL_LATIN1_GENERAL_CP1_CI_AS" | ||||||||||
} | ||||||||||
|
||||||||||
variable "sql_database_sku" { | ||||||||||
type = string | ||||||||||
default = "GP_Gen5_2" | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,19 @@ | ||||||
provider "azurerm" { | ||||||
features {} | ||||||
} | ||||||
|
||||||
module "mssql-server" { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
source = "../src" | ||||||
|
||||||
environment = "baz" | ||||||
identifier = "qux" | ||||||
location = "uksouth" | ||||||
zone = "waldo" | ||||||
|
||||||
log_analytics_workspace_id = "quux" | ||||||
sql_server_id = "123" | ||||||
|
||||||
tags = { | ||||||
Foo = "Bar" | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
terraform { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file in |
||
required_version = "~> 1.5" | ||
|
||
required_providers { | ||
azurerm = { | ||
source = "hashicorp/azurerm" | ||
version = "~> 3.85" | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.