From a7ced94d04f58004a4f80b67d053ec80bac7dfac Mon Sep 17 00:00:00 2001 From: Craig Anderson <1877372+CDA0@users.noreply.github.com> Date: Fri, 5 Jan 2024 16:27:00 +0000 Subject: [PATCH] Add MySQL Flexible Server module (#30) --- modules/mysql-flexible-server/CHANGELOG.md | 1 + modules/mysql-flexible-server/README.md | 32 ++++++++++ modules/mysql-flexible-server/VERSION | 1 + modules/mysql-flexible-server/src/locals.tf | 14 +++++ modules/mysql-flexible-server/src/main.tf | 45 +++++++++++++ modules/mysql-flexible-server/src/outputs.tf | 7 +++ .../mysql-flexible-server/src/variables.tf | 63 +++++++++++++++++++ modules/mysql-flexible-server/test/main.tf | 20 ++++++ .../mysql-flexible-server/test/terraform.tf | 10 +++ 9 files changed, 193 insertions(+) create mode 100644 modules/mysql-flexible-server/CHANGELOG.md create mode 100644 modules/mysql-flexible-server/README.md create mode 100644 modules/mysql-flexible-server/VERSION create mode 100644 modules/mysql-flexible-server/src/locals.tf create mode 100644 modules/mysql-flexible-server/src/main.tf create mode 100644 modules/mysql-flexible-server/src/outputs.tf create mode 100644 modules/mysql-flexible-server/src/variables.tf create mode 100644 modules/mysql-flexible-server/test/main.tf create mode 100644 modules/mysql-flexible-server/test/terraform.tf diff --git a/modules/mysql-flexible-server/CHANGELOG.md b/modules/mysql-flexible-server/CHANGELOG.md new file mode 100644 index 0000000..825c32f --- /dev/null +++ b/modules/mysql-flexible-server/CHANGELOG.md @@ -0,0 +1 @@ +# Changelog diff --git a/modules/mysql-flexible-server/README.md b/modules/mysql-flexible-server/README.md new file mode 100644 index 0000000..d59e7d9 --- /dev/null +++ b/modules/mysql-flexible-server/README.md @@ -0,0 +1,32 @@ +# MySQL Flexible Server + +This module creates a [MySQL Flexible Server](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/mysql_flexible_server) and associated [Diagnostic Setting](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/monitor_diagnostic_setting). + +## Usage + +```hcl +module "mysql_flexible_server" { + source = "https://github.com/gofrontier-com/azurerm-terraform-modules/releases/download/mysql-flexible-server/[VERSION]/module.tar.gz//src" + + administrator_login = "mysqladmin" + administrator_password = "P@ssw0rd1234!" + environment = "dev" + identifier = "mortgages" + location = "uksouth" + log_analytics_workspace_id = data.azurerm_log_analytics_workspace.main.id + resource_group_name = module.resource_group.name + zone = "mtg" + + tags = { + WorkloadType = "MortgagesLZ/data-platform" + } +} +``` + +## Known issues + +_None._ + +## Contributing + +See . diff --git a/modules/mysql-flexible-server/VERSION b/modules/mysql-flexible-server/VERSION new file mode 100644 index 0000000..d3827e7 --- /dev/null +++ b/modules/mysql-flexible-server/VERSION @@ -0,0 +1 @@ +1.0 diff --git a/modules/mysql-flexible-server/src/locals.tf b/modules/mysql-flexible-server/src/locals.tf new file mode 100644 index 0000000..6a78776 --- /dev/null +++ b/modules/mysql-flexible-server/src/locals.tf @@ -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 + } +} diff --git a/modules/mysql-flexible-server/src/main.tf b/modules/mysql-flexible-server/src/main.tf new file mode 100644 index 0000000..71975f8 --- /dev/null +++ b/modules/mysql-flexible-server/src/main.tf @@ -0,0 +1,45 @@ +resource "azurerm_mysql_flexible_server" "main" { + name = "mysql-${var.zone}-${var.environment}-${lookup(local.short_locations, var.location)}-${local.identifier}" + resource_group_name = var.resource_group_name + location = var.location + + administrator_login = var.administrator_login + administrator_password = var.administrator_password + backup_retention_days = var.backup_retention_days + sku_name = var.sku_name + + tags = merge(var.tags, local.tags) +} + +resource "azurerm_monitor_diagnostic_setting" "main" { + name = "log-analytics" + target_resource_id = azurerm_mysql_flexible_server.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 + + retention_policy { + days = 0 + enabled = false + } + } + } + + dynamic "metric" { + for_each = var.metric_categories + + content { + category = metric.value + + retention_policy { + days = 0 + enabled = false + } + } + } +} diff --git a/modules/mysql-flexible-server/src/outputs.tf b/modules/mysql-flexible-server/src/outputs.tf new file mode 100644 index 0000000..e6cb829 --- /dev/null +++ b/modules/mysql-flexible-server/src/outputs.tf @@ -0,0 +1,7 @@ +output "id" { + value = azurerm_mysql_flexible_server.main.id +} + +output "name" { + value = azurerm_mysql_flexible_server.main.name +} diff --git a/modules/mysql-flexible-server/src/variables.tf b/modules/mysql-flexible-server/src/variables.tf new file mode 100644 index 0000000..ba7789b --- /dev/null +++ b/modules/mysql-flexible-server/src/variables.tf @@ -0,0 +1,63 @@ +variable "administrator_login" { + type = string + default = "sqladmin" +} + +variable "administrator_password" { + type = string +} + +variable "backup_retention_days" { + type = number + default = 7 +} + +variable "environment" { + type = string +} + +variable "identifier" { + type = string +} + +variable "location" { + type = string +} + +variable "log_analytics_workspace_id" { + type = string +} + +# https://learn.microsoft.com/en-us/azure/azure-monitor/reference/supported-logs/microsoft-dbformysql-flexibleservers-logs +variable "log_categories" { + type = list(string) + default = [ + "MySqlAuditLogs", + "MySqlSlowLogs" + ] +} + +variable "metric_categories" { + type = list(string) + default = [ + "AllMetrics" + ] +} + +variable "resource_group_name" { + type = string +} + +variable "sku_name" { + type = string + default = "Standard_D2ds_v4" +} + +variable "tags" { + type = map(string) + default = {} +} + +variable "zone" { + type = string +} diff --git a/modules/mysql-flexible-server/test/main.tf b/modules/mysql-flexible-server/test/main.tf new file mode 100644 index 0000000..39dbae4 --- /dev/null +++ b/modules/mysql-flexible-server/test/main.tf @@ -0,0 +1,20 @@ +provider "azurerm" { + features {} +} + +module "mysql_flexible_server" { + source = "../src" + + administrator_login = "mysqladmin" + administrator_password = "P@ssw0rd1234!" + environment = "baz" + identifier = "qux" + location = "uksouth" + log_analytics_workspace_id = "quux" + resource_group_name = "grault" + zone = "waldo" + + tags = { + Foo = "Bar" + } +} diff --git a/modules/mysql-flexible-server/test/terraform.tf b/modules/mysql-flexible-server/test/terraform.tf new file mode 100644 index 0000000..762ef2e --- /dev/null +++ b/modules/mysql-flexible-server/test/terraform.tf @@ -0,0 +1,10 @@ +terraform { + required_version = "~> 1.5" + + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = "~> 3.85" + } + } +}