Skip to content

Commit

Permalink
Add MySQL Flexible Server module (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
CDA0 authored Jan 5, 2024
1 parent aa96935 commit a7ced94
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 0 deletions.
1 change: 1 addition & 0 deletions modules/mysql-flexible-server/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Changelog
32 changes: 32 additions & 0 deletions modules/mysql-flexible-server/README.md
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/gofrontier-com/azurerm-terraform-modules/blob/main/README.rst#contributing>.
1 change: 1 addition & 0 deletions modules/mysql-flexible-server/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.0
14 changes: 14 additions & 0 deletions modules/mysql-flexible-server/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
}
}
45 changes: 45 additions & 0 deletions modules/mysql-flexible-server/src/main.tf
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}
7 changes: 7 additions & 0 deletions modules/mysql-flexible-server/src/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
output "id" {
value = azurerm_mysql_flexible_server.main.id
}

output "name" {
value = azurerm_mysql_flexible_server.main.name
}
63 changes: 63 additions & 0 deletions modules/mysql-flexible-server/src/variables.tf
Original file line number Diff line number Diff line change
@@ -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
}
20 changes: 20 additions & 0 deletions modules/mysql-flexible-server/test/main.tf
Original file line number Diff line number Diff line change
@@ -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"
}
}
10 changes: 10 additions & 0 deletions modules/mysql-flexible-server/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 a7ced94

Please sign in to comment.