아래와 같이 terraform에 사용할 변수 및 main.tf를 작성후 아래 명령어를 사용하여 생성 가능
terraform init # 작업 환경 setup
terraform apply # 작성된 내용대로 리소스 생성
1. variables.tf
variable "azure-subscription-id" {
type = string
description = "Azure Subscription ID"
default = "" # prod
}
variable "location" {
type = string
description = "resource location"
default = "Southeast Asia"
}
variable "resource-name" {
type = string
description = "resource name"
default = "prd-rg-test-seas"
}
variable "log-resource-name" {
type = string
description = "resource name"
default = "prd-rg-common-seas"
}
variable "server-name" {
type = list(string)
description = "mysql server name"
}
variable "log-name" {
type = string
description = "log analytics name"
default = "prd-log-seas-db"
}
variable "sku-name" {
type = string
description = "server spec"
default = "GP_Standard_D2ds_v4"
}
variable "delegate-subnet" {
type = string
description = "flexible mysql subnet"
default = "/subscriptions/??/resourceGroups/prd-rg-common-seas/providers/Microsoft.Network/virtualNetworks/prd-vnet-seas/subnets/prd-svnet-flexibledb-seas"
}
variable "private-dns" {
type = string
description = "private dsn zone in virtual network"
default = "/subscriptions/??/resourceGroups/prd-rg-common/providers/Microsoft.Network/privateDnsZones/privatelink.mysql.database.azure.com"
}
variable "mysql-admin-login" {
type = string
description = "MySQL Admin User"
default = ""
}
variable "mysql-admin-pass" {
type = string
description = "MySQL Admin Password"
default = ""
}
variable "mysql-version" {
type = string
description = "MySQL Version"
default = "8.0.21"
}
2. main.tf
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "=3.0.0"
}
}
}
provider "azurerm" {
features {}
subscription_id = var.azure-subscription-id
tenant_id = ""
}
resource "azurerm_mysql_flexible_server" "flexible_server" {
for_each = toset(var.server-name)
name = each.value
resource_group_name = var.resource-name
location = var.location
administrator_login = var.mysql-admin-login
administrator_password = var.mysql-admin-pass
delegated_subnet_id = var.delegate-subnet
private_dns_zone_id = var.private-dns
backup_retention_days = 7
sku_name = var.sku-name
version = var.mysql-version
storage {
size_gb = 32
}
high_availability {
mode = "ZoneRedundant"
standby_availability_zone = "2"
}
zone = "1"
}
resource "azurerm_mysql_flexible_server_configuration" "config_timeout" {
for_each = toset(var.server-name)
name = "connect_timeout"
resource_group_name = var.resource-name
server_name = each.value
value = "60"
depends_on = [ azurerm_mysql_flexible_server.flexible_server ]
}
resource "azurerm_mysql_flexible_server_configuration" "config_isolation" {
for_each = toset(var.server-name)
name = "transaction_isolation"
resource_group_name = var.resource-name
server_name = each.value
value = "READ-COMMITTED"
depends_on = [ azurerm_mysql_flexible_server.flexible_server ]
}
resource "azurerm_mysql_flexible_server_configuration" "config_character" {
for_each = toset(var.server-name)
name = "character_set_server"
resource_group_name = var.resource-name
server_name = each.value
value = "UTF8MB4"
depends_on = [ azurerm_mysql_flexible_server.flexible_server ]
}
resource "azurerm_mysql_flexible_server_configuration" "config_collation" {
for_each = toset(var.server-name)
name = "collation_server"
resource_group_name = var.resource-name
server_name = each.value
value = "UTF8MB4_BIN"
depends_on = [ azurerm_mysql_flexible_server.flexible_server ]
}
resource "azurerm_mysql_flexible_server_configuration" "config_infile" {
for_each = toset(var.server-name)
name = "local_infile"
resource_group_name = var.resource-name
server_name = each.value
value = "OFF"
depends_on = [ azurerm_mysql_flexible_server.flexible_server ]
}
resource "azurerm_mysql_flexible_server_configuration" "config_querytime" {
for_each = toset(var.server-name)
name = "long_query_time"
resource_group_name = var.resource-name
server_name = each.value
value = "1"
depends_on = [ azurerm_mysql_flexible_server.flexible_server ]
}
resource "azurerm_mysql_flexible_server_configuration" "config_maxconn" {
for_each = toset(var.server-name)
name = "max_connections"
resource_group_name = var.resource-name
server_name = each.value
value = "1365"
depends_on = [ azurerm_mysql_flexible_server.flexible_server ]
}
resource "azurerm_mysql_flexible_server_configuration" "config_audit" {
for_each = toset(var.server-name)
name = "audit_log_enabled"
resource_group_name = var.resource-name
server_name = each.value
value = "ON"
depends_on = [ azurerm_mysql_flexible_server.flexible_server ]
}
resource "azurerm_mysql_flexible_server_configuration" "config_slow" {
for_each = toset(var.server-name)
name = "slow_query_log"
resource_group_name = var.resource-name
server_name = each.value
value = "ON"
depends_on = [ azurerm_mysql_flexible_server.flexible_server ]
}
resource "azurerm_log_analytics_workspace" "logs" {
name = var.log-name
location = var.location
resource_group_name = var.log-resource-name
retention_in_days = 60
}
resource "azurerm_monitor_diagnostic_setting" "monitor" {
for_each = toset(var.server-name)
name = lower("${each.value}-diag")
target_resource_id = azurerm_mysql_flexible_server.flexible_server[each.value].id
log_analytics_workspace_id = azurerm_log_analytics_workspace.logs.id
metric {
category = "AllMetrics"
enabled = false
}
log {
category = "MySqlAuditLogs"
enabled = true
}
log {
category = "MySqlSlowLogs"
enabled = true
}
lifecycle {
ignore_changes = [metric]
}
depends_on = [ azurerm_mysql_flexible_server.flexible_server ]
}
3. output.tf
output "mysql_server" {
value = [
for serv in azurerm_mysql_flexible_server.flexible_server : serv.id
]
}
output "mysql_server_config" {
value = azurerm_mysql_flexible_server_configuration.config_isolation
}
output "mysql_fqdn" {
value = [
for serv in azurerm_mysql_flexible_server.flexible_server : serv.fqdn
]
}
'cloud > azure' 카테고리의 다른 글
azure cloud 기본 구조 (0) | 2022.08.10 |
---|---|
azure log analytics 또는 log 에서 로그 확인 SQL (0) | 2022.06.15 |
테라폼 반복문 및 조건문 (0) | 2022.03.22 |
proxysql in azure (0) | 2022.02.09 |
azure for mysql replication 구성 (0) | 2022.02.04 |