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

Module for Azure MySQL DB #49

Open
wants to merge 8 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
78 changes: 78 additions & 0 deletions terraform/azure/examples/mysql-db/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@


provider "azurerm" {
features {}
}

module "mysql_db" {
source = "../modules/mysql-db"

# Add all required variables for the module
vnet_name = "my-vnet"
vnet_address_space = ["10.0.0.0/16"]
location = "Central India"
resource_group_name = "sample-resource-group"
subnet_name = "test-subnet"
subnet_address_prefixes = ["10.0.1.0/24", "10.0.2.0/24"]
nsg_name = "my-nsg"

inbound_rules = [
{
name = "allow_ssh"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "22"
source_address_prefix = "*"
destination_address_prefix = "*"
}
]

outbound_rules = [
{
name = "allow_all"
priority = 100
direction = "Outbound"
access = "Allow"
protocol = "*"
source_port_range = "*"
destination_port_range = "*"
source_address_prefix = "*"
destination_address_prefix = "*"
}
]

subnet_service_endpoints = ["Microsoft.Storage"]
subnet_delegation_name = "test-delegation"
subnet_service_delegation_name = "Microsoft.DBforMySQL/flexibleServers"
subnet_service_delegation_actions = ["Microsoft.Network/virtualNetworks/subnets/join/action"]
private_dns_zone_name = "test.mysql.database.azure.com"
private_dns_zone_link_name = "sampleVnetZone.com"
mysql_server_name = "samplesqlserver02"
mysql_admin_login = "mysqladmin"
mysql_admin_password = "Password@123"
mysql_backup_retention_days = 7
mysql_sku_name = "B_Standard_B1s"
mysql_zone = "2"
mysql_database_name = "sample-mysql-db"
mysql_database_charset = "utf8"
mysql_database_collation = "utf8_general_ci"
mysql_private_endpoint_name = "myPrivateEndpoint"
mysql_private_endpoint_connection_name = "myPrivateConnection"
environment = "dev"
mysql_firewall_rules = [
{
name = "AllowAllWindowsAzureIps"
start_ip_address = "0.0.0.0"
end_ip_address = "0.0.0.0"
},
{
name = "AllowMyIP"
start_ip_address = "123.456.789.0"
end_ip_address = "123.456.789.0"
}
]
}

20 changes: 20 additions & 0 deletions terraform/azure/examples/mysql-db/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@


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

provider "azurerm" {
features {}

client_id = " "
client_secret = " "
tenant_id = " "
subscription_id = " "
skip_provider_registration = true
}
87 changes: 87 additions & 0 deletions terraform/azure/modules/mysql-db/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Terraform Module: Azure MySQL Flexible Server

This Terraform module creates an Azure MySQL Flexible Server along with a MySQL database and optional firewall rules.

## Module Usage

To use this module, create a new Terraform configuration file and include the module:

```hcl
provider "azurerm" {
features {}
}

module "azure_sql_db_flexible" {
source = "./terraform-azure-sql-db-flexible"

resource_group_name = "sample-resource-group"
location = "Central India"
virtual_network_name = "my-vnet"
address_space = ["10.0.0.0/16"]
subnet_name = "my-subnet"
subnet_address_prefixes = ["10.0.2.0/24"]
subnet_service_endpoints = ["Microsoft.Storage"]
subnet_delegation_name = "test-delegation"
subnet_service_delegation_name = "Microsoft.DBforMySQL/flexibleServers"
subnet_service_delegation_actions = ["Microsoft.Network/virtualNetworks/subnets/join/action"]
private_dns_zone_name = "test.mysql.database.azure.com"
private_dns_zone_link_name = "sampleVnetZone.com"
mysql_server_name = "sample-mysql-server"
mysql_admin_login = "mysqladmin"
mysql_admin_password = "Password@123"
mysql_backup_retention_days = 7
mysql_sku_name = "B_Standard_B1s"
mysql_zone = "2"
mysql_database_name = "sample-mysql-db"
mysql_database_charset = "utf8"
mysql_database_collation = "utf8_general_ci"
mysql_firewall_rules = [
{
name = "AllowAllWindowsAzureIps"
start_ip_address = "0.0.0.0"
end_ip_address = "0.0.0.0"
},
{
name = "AllowMyIP"
start_ip_address = "123.456.789.0"
end_ip_address = "123.456.789.0"
}
]
}
```


## Inputs

| Name | Description | Type | Default | Required |
|------------------------|----------------------------------------------|------------|---------|----------|
| `resource_group_name` | Name of the Azure Resource Group. | `string` | n/a | Yes |
| `vnet_name` | Name of the Azure Virtual Network. | `string` | n/a | Yes |
| `address_space` | Address space for the VNet. | `list(string)` |n/a | Yes |
| `location` | Azure region where resources will be created.| `string` | n/a | Yes |
| `subnet_names` | List of subnet names. | `list(string)` | n/a | Yes |
| `subnet_address_prefixes` | List of subnet address prefixes. | `list(string)` | n/a | Yes |
| `subnet_service_endpoints`| The service endpoints for the Azure subnet.| `list(string)` | n/a |Yes |
| `subnet_delegation_name` | The name of the subnet delegation. | `string` | n/a | yes |
| `subnet_service_delegation_name` | The service delegation name for the subnet. | `string` | n/a | yes |
| `subnet_service_delegation_actions` | The actions allowed for the subnet service delegation. | `list(string)` | n/a | yes |
| `private_dns_zone_name` | The name of the Azure private DNS zone. | `string` | n/a | yes |
| `private_dns_zone_link_name` | The name of the virtual network link to private DNS zone. | `string` | n/a | yes |
| `mysql_server_name` | The name of the MySQL Server. | `string` | n/a | yes |
| `mysql_admin_login` | The administrator login name for the MySQL server. | `string` | n/a | yes |
| `mysql_admin_password` | The password associated with the MySQL administrator login. | `string` | n/a | yes |
| `mysql_backup_retention_days` | The backup retention days for the MySQL server. | `number` | 7 | yes |
| `mysql_sku_name` | The SKU name for the MySQL server. | `string` | "GP_Standard_D2ds_v4" | yes |
| `mysql_zone` | The availability zone for the MySQL server. | `string` | "2" | yes |
| `mysql_database_name` | The name of the MySQL database. | `string` | n/a | yes |
| `mysql_database_charset` | The charset for the MySQL database. | `string` | "utf8" | yes |
| `mysql_database_collation` | The collation for the MySQL database. | `string` | "utf8_general_ci" | yes |


## Outputs

| Name | Description |
|-------------|---------------------------------------------------|
| `mysql_server_name` | The name of the MySQL server. |
| `mysql_server_fully_qualified_domain_name` | The fully qualified domain name of the MySQL server. |
| `mysql_database_name` | The name of the MySQL database. |
78 changes: 78 additions & 0 deletions terraform/azure/modules/mysql-db/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@


provider "azurerm" {
features {}
}

# Module for network
module "network" {
source = "../vnet"
vnet_name = var.vnet_name
address_space = var.vnet_address_space
location = var.location
resource_group_name = var.resource_group_name
subnet_names = var.subnet_names
subnet_address_prefixes = var.subnet_address_prefixes
nsg_name = var.nsg_name
inbound_rules = var.inbound_rules
outbound_rules = var.outbound_rules
environment = var.environment
}

resource "azurerm_private_dns_zone" "domain" {
name = var.private_dns_zone_name
resource_group_name = var.resource_group_name
}

resource "azurerm_private_dns_zone_virtual_network_link" "vlink" {
name = var.private_dns_zone_link_name
private_dns_zone_name = azurerm_private_dns_zone.domain.name
virtual_network_id = module.network.vnet_id
resource_group_name = var.resource_group_name
}

resource "azurerm_mysql_flexible_server" "sqlserver" {
name = var.mysql_server_name
resource_group_name = var.resource_group_name
location = var.location
administrator_login = var.mysql_admin_login
administrator_password = var.mysql_admin_password
backup_retention_days = var.mysql_backup_retention_days
delegated_subnet_id = module.network.subnet_ids[0] # Using the first subnet ID
private_dns_zone_id = azurerm_private_dns_zone.domain.id
sku_name = var.mysql_sku_name
zone = var.mysql_zone

depends_on = [azurerm_private_dns_zone_virtual_network_link.vlink]
}

resource "azurerm_mysql_flexible_database" "sqldb" {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I might be wrong but this can only create a flexible database. Please add options to create azurerm_sql_server based on the user's requirement.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Single Server service, it can no longer handle all the new features, functions, and security needs and Azure Database for MySQL - Single Server is scheduled for retirement by September 16, 2024. We have 2 options while creating azure mysql db one is flexible and another one is word-press + flexible server

name = var.mysql_database_name
resource_group_name = var.resource_group_name
server_name = azurerm_mysql_flexible_server.sqlserver.name
charset = var.mysql_database_charset
collation = var.mysql_database_collation
}

resource "azurerm_mysql_flexible_server_firewall_rule" "firewall" {
name = var.mysql_firewall_rule_name
resource_group_name = var.resource_group_name
server_name = azurerm_mysql_flexible_server.sqlserver.name
start_ip_address = var.mysql_firewall_start_ip
end_ip_address = var.mysql_firewall_end_ip
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Also please add options to create VNET private links.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Similaryl add as many features as possible, tht you feel will be needed,

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added private endpoint


# Private Endpoint
resource "azurerm_private_endpoint" "mysql_private_endpoint" {
name = var.mysql_private_endpoint_name
location = var.location
resource_group_name = var.resource_group_name
subnet_id = module.network.subnet_ids[0] # Using the first subnet ID

private_service_connection {
name = var.mysql_private_endpoint_connection_name
private_connection_resource_id = azurerm_mysql_flexible_server.sqlserver.id
subresource_names = ["mysqlserver"]
is_manual_connection = false
}
}
9 changes: 9 additions & 0 deletions terraform/azure/modules/mysql-db/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@


output "mysql_server_fqdn" {
value = azurerm_mysql_flexible_server.sqlserver.fqdn
}

output "mysql_database_id" {
value = azurerm_mysql_flexible_database.sqldb.id
}
Loading