如何用Terraform配置Azure PostgreSQL弹性服务器维护通知及测试?
Azure PostgreSQL弹性服务器计划维护邮件通知配置(Terraform)
配置步骤
要实现维护事件邮件通知,需要在Terraform中配置两个核心资源:动作组(Action Group)和活动日志警报(Activity Log Alert),前者定义通知接收方式,后者指定触发通知的维护事件条件。
1. 定义动作组
动作组负责将警报通知发送到指定邮箱,示例代码如下:
resource "azurerm_monitor_action_group" "pg_maintenance_notification" { name = "pg-maintenance-notify" resource_group_name = azurerm_postgresql_flexible_server.pg_server.resource_group_name short_name = "pg-maintain" email_receiver { name = "admin-notify" email_address = "your-notification-email@example.com" use_common_alert_schema = true } }
- 替换
your-notification-email@example.com为实际接收邮件的地址 resource_group_name需与你的PostgreSQL服务器所属资源组一致,示例中直接引用已有服务器的资源组属性
2. 配置活动日志警报
针对PostgreSQL弹性服务器的计划维护事件创建警报,触发时调用上述动作组发送邮件:
resource "azurerm_monitor_activity_log_alert" "pg_maintenance_alert" { name = "pg-maintenance-alert" resource_group_name = azurerm_postgresql_flexible_server.pg_server.resource_group_name scopes = [azurerm_postgresql_flexible_server.pg_server.id] criteria { category = "ServiceHealth" operation_name { value = "Microsoft.DBforPostgreSQL/flexibleServers/plannedMaintenanceNotifications/action" } resource_type { value = "Microsoft.DBforPostgreSQL/flexibleServers" } } action { action_group_id = azurerm_monitor_action_group.pg_maintenance_notification.id } }
scopes指定监控的目标资源,直接关联你的PostgreSQL弹性服务器- 条件中的
operation_name是Azure针对PostgreSQL弹性服务器计划维护事件的固定操作名称,确保只触发维护相关通知
3. 部署配置
将上述代码添加到你现有的Terraform配置文件中,与已有的azurerm_postgresql_flexible_server资源放在一起,依次执行terraform init、terraform plan验证配置,确认无误后执行terraform apply完成部署。
功能验证说明
配置完成后,无法直接模拟触发计划维护事件来测试警报(这类事件由Azure平台生成),但可以通过以下方式验证通知通道有效性:
- 登录Azure门户,找到创建的动作组,点击「测试动作组」,选择「Service Health」类型的测试警报,系统会向指定邮箱发送测试邮件,以此确认邮件通知通道正常工作。
内容的提问来源于stack exchange,提问作者Ikteaja hsan
相关产品推荐
相关产品推荐

