0

I have deployed a postgreesql server using terraform. I have configured schedule maintenance.

maintenance_window {
    day_of_week = 0 // Sunday
    start_hour = 21 
    start_minute = 0
  }

Now i want to enable notifications through mail address for upcoming scheduled maintenance events using terraform for azure postgresql flexible server..could you please guide me how can i configure it through the terraform.is it possible to send a test event notification right after the configuration to check whether is it enabled or not ?. I really appreciate any positive reply and thanks in advance.

eku
  • 11
  • 1
  • 2
  • 6

1 Answers1

0

I tried to add the monitoring and email notifications for the postgresssql flexible server and got the below output

I have added the following script to get the email and notifications for flexible server

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "RG_NAME"
  location = "EASTUS"
}

resource "azurerm_virtual_network" "example" {
  name                = "example-vn"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  address_space       = ["10.0.0.0/16"]
}

resource "azurerm_subnet" "example" {
  name                 = "example-sn"
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.example.name
  address_prefixes     = ["10.0.2.0/24"]
  service_endpoints    = ["Microsoft.Storage"]
  delegation {
    name = "fs"
    service_delegation {
      name = "Microsoft.DBforPostgreSQL/flexibleServers"
      actions = [
        "Microsoft.Network/virtualNetworks/subnets/join/action",
      ]
    }
  }
}
resource "azurerm_private_dns_zone" "example" {
  name                = "example.postgres.database.azure.com"
  resource_group_name = azurerm_resource_group.example.name
}

resource "azurerm_private_dns_zone_virtual_network_link" "example" {
  name                  = "exampleVnetZone.com"
  private_dns_zone_name = azurerm_private_dns_zone.example.name
  virtual_network_id    = azurerm_virtual_network.example.id
  resource_group_name   = azurerm_resource_group.example.name
}

resource "azurerm_postgresql_flexible_server" "example" {
  name                   = "example-psqlflexibleserver"
  resource_group_name    = azurerm_resource_group.example.name
  location               = azurerm_resource_group.example.location
  version                = "12"
  delegated_subnet_id    = azurerm_subnet.example.id
  private_dns_zone_id    = azurerm_private_dns_zone.example.id
  administrator_login    = "psqladmin"
  administrator_password = "H@Sh1CoR3!"
  zone                   = "1"

  storage_mb = 32768
  backup_retention_days = 30
  geo_redundant_backup_enabled = true

  sku_name   = "GP_Standard_D4s_v3"
  depends_on = [azurerm_private_dns_zone_virtual_network_link.example]
 
}


resource "azurerm_postgresql_flexible_server" "examplez" {
  administrator_login    = "psqladmin"
  administrator_password = "H@Sh1CoR3!"
  name  =  "dbserverex"
  resource_group_name= "RG_NAME"
  location  =  "eastus"
  storage_mb = 32768
  backup_retention_days  =  30
  geo_redundant_backup_enabled = true
  sku_name = "GP_Standard_D4s_v3"
  
  depends_on = [azurerm_private_dns_zone_virtual_network_link.example]

}

resource "azurerm_postgresql_flexible_server" "dbtomonitor" {
  resource_group_name = "RG_NAME"
  name = "testdb"
  location = "eastus"
  geo_redundant_backup_enabled = true

 sku_name = "GP_Standard_D4s_v3"
   depends_on = [azurerm_private_dns_zone_virtual_network_link.example]
}    
resource "azurerm_monitor_metric_alert" "example" {
  name = "example-metricalert"
  resource_group_name = azurerm_postgresql_flexible_server.examplez.resource_group_name
  scopes = [azurerm_postgresql_flexible_server.dbtomonitor.id]
  description = "action will be  triggered when cpu percent is greater than 80."
  criteria {
    metric_namespace = "Microsoft.Sql/servers/databases"
    metric_name      = "cpu_percent"
    aggregation      = "Average"
    operator         = "GreaterThan"
    threshold        = 80
  }
 action {
    action_group_id = azurerm_monitor_action_group.example.id
  }
}
resource "azurerm_monitor_action_group" "example" {
  name                = "CriticalAlertsAction"
  resource_group_name = data.azurerm_mssql_server.example.resource_group_name
  short_name          = "p0action"

  email_receiver {
    name                    = "komaliXXXXXX@.com"
    email_address           = "youremailid"
    use_common_alert_schema = true
  }
}

After adding the above script run the below steps to execute the terraform file

terraform init

This will initialise the file

enter image description here

Terraform plan

This will creates an execution plan and it will preview the changes that terraform plans to make the infrastructure it will show the monitoring and email notification rules

enter image description here

terraform apply

This will creates or updates the infrastructure depending on the configuration and also creates the metric rules for the flexible server

enter image description here

For more information use this reference link

NOTE: Please make sure while writing the script, resource group , version, sku_name, admin credentials, location, storage_mb, email should be given

Komali Annem
  • 649
  • 1
  • 1
  • 7
  • is this resource azurerm_monitor_metric_alert also trigger the upcoming scheduled maintenance alert or do i have to create specific resource for exampl e "azurerm_monitor_scheduled_query_rules_alert" "schedule_maintenance_alert" – eku Nov 14 '22 at 11:12
  • @eku thanks for the reply, ```resource azurerm_monitor_metric_alert``` it will not trigger the upcoming schedule maintenance, we have to create the specific resource like ```azurerm_monitor_scheduled_query_rules_alert```. – Komali Annem Nov 14 '22 at 11:22
  • where, can i share my schedule maintenance code? in the comment i cant :( – eku Nov 14 '22 at 13:18