【问题标题】:Create Azure service health alert in Terraform在 Terraform 中创建 Azure 服务运行状况警报
【发布时间】:2021-03-31 16:53:25
【问题描述】:

我刚刚发现,如果我们使用 Terraform 创建 Azure 服务健康警报,代码如下:

resource "azurerm_monitor_activity_log_alert" "servicehealth" {
  name                = "${var.client_initial}-MCS Maintain Service Health"
  description         = "${var.client_initial}-MCS Maintain Service Health Alerts"
  resource_group_name = var.resource_group_name
  scopes              = [var.scopes]
  criteria {
    category = "ServiceHealth"
  }
  tags = var.tags
  action {
    action_group_id = var.action_group_id
  }
}

当我执行terraform apply 时,它适用,但是当我检查门户时,没有选择任何区域。你不能通过门户做同样的事情,如果你通过门户做,你必须选择区域或选择所有区域。

那么如果通过 Terraform 部署并且没有选择区域,这是否意味着它适用于所有区域?

我在 GitHub 上看到,对此进行更精细的控制仍然是一个悬而未决的问题 https://github.com/terraform-providers/terraform-provider-azurerm/issues/2996

【问题讨论】:

  • 关于这个问题还有更多更新吗?它解决了你的问题吗?如果解决了您的问题,请采纳。

标签: terraform terraform-provider-azure azure-monitoring


【解决方案1】:

地区将根据

resource_group_name = var.resource_group_name

其中resource_group_name 需要azurerm_resource_group 的实例:

resource "azurerm_resource_group" "example" {
  name     = "example"
  location = "West Europe"
}

location -(必需)资源组应存在的 Azure 区域。更改此设置会强制创建一个新的资源组。

【讨论】:

  • 嗨,伙计,感谢 cmets,所以我的 resource_group_name 在 australia_east。但是,australia_east 的 service health region 没有打勾?那里没有打勾
【解决方案2】:

据我所知,活动日志警报的位置始终是Global。可以在三个级别创建活动日志警报:资源级别、资源组级别和订阅级别。资源组和订阅都可以包含多个区域的资源。而且你也不能在创建时设置位置。不确定,但似乎活动日志警报除了Global 之外没有特殊区域。

【讨论】:

  • 嗨,查尔斯,感谢您的回答,但我认为这不是我想要的。范围设置为整个订阅,我要问的是为什么 terraform 在没有选择任何区域的情况下创建服务健康警报?这是否意味着所有地区都将受到监控?
  • @RogerChen 当然,是的。正如我所说,所有警报都具有相同的区域Global,这意味着它。
  • 您好,查尔斯,感谢您的回答。还有一些问题。当您说活动日志警报仅具有全局区域时,但实际上,在创建服务健康警报时,它确实具有区域过滤器以向下钻取特定区域。那么这是否意味着它会覆盖全球区域?
  • @RogerChen 是的,就是覆盖全局区域。如果没有,它只能过滤单个区域,而不是多个。
【解决方案3】:

在使用 Terraform 设置 Azure Monitor Service Health Alert 时遇到了类似的挑战。

我是这样做的:

模块主文件

resource "azurerm_monitor_activity_log_alert" "main" {
  name                = var.monitor_activity_log_alert
  resource_group_name = var.resource_group_name
  scopes              = var.monitor_activity_log_alert_scope
  description         = var.monitor_activity_log_alert_description
  enabled             = var.monitor_activity_log_alert_enabled

  criteria {
    category = var.criteria_category

    service_health {
      events    = var.service_health_events
      locations = var.service_health_locations
      services  = var.service_health_services
    }
  }

  action {
    action_group_id = var.action_group_id
  }

  tags = {
    Environment  = var.tag_environment
    BillingGroup = var.tag_billing_group
  }
}

模块变量文件

variable "monitor_activity_log_alert" {
  type        = string
  description = "The name of the activity log alert"
}

variable "resource_group_name" {
  type        = string
  description = "The name of the resource group in which to create the activity log alert instance."
}

variable "monitor_activity_log_alert_scope" {
  type        = list(string)
  description = "The Scope at which the Activity Log should be applied, for example a the Resource ID of a Subscription or a Resource (such as a Storage Account)."
}

variable "monitor_activity_log_alert_description" {
  type        = string
  description = "The description of this activity log alert."
}

variable "monitor_activity_log_alert_enabled" {
  type        = bool
  description = "Should this Activity Log Alert be enabled? Defaults to true."
}

variable "criteria_category" {
  type        = string
  description = "The category of the operation. Possible values are Administrative, Autoscale, Policy, Recommendation, ResourceHealth, Security and ServiceHealth."
}

variable "service_health_events" {
  type        = list(string)
  description = "Events this alert will monitor Possible values are Incident, Maintenance, Informational, ActionRequired and Security. Defaults to all Events"
}

variable "service_health_locations" {
  type        = list(string)
  description = "Locations this alert will monitor. For example, West Europe. Defaults to Global."
}

variable "service_health_services" {
  type        = list(string)
  description = "Services this alert will monitor. For example, Activity Logs & Alerts, Action Groups. Defaults to all Services."
}

variable "action_group_id" {
  type        = string
  description = "The ID of the Action Group can be sourced from the azurerm_monitor_action_group resource"
}

variable "tag_environment" {
  type        = string
  description = "A mapping of tags which should be assigned to the resource."
}

variable "tag_billing_group" {
  type        = string
  description = "A mapping of tags which should be assigned to the resource."
}

创建资源的模块主文件

terraform {
  required_version = "~> 1.0.8"

  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "2.81.0"
    }
  }

  backend "azurerm" {
    resource_group_name  = "MyGlobalRG"
    storage_account_name = "myterraform"
    container_name       = "terraform-state-files"
    key                  = "azure-resources/global/monitor-activity-log-alert/terraform.tfstate"
  }
}

provider "azurerm" {
  features {}
}

data "azurerm_subscription" "current" {
}

data "azurerm_resource_group" "main" {
  name = var.resource_group_name
}

data "azurerm_monitor_action_group" "main" {
  name                = var.monitor_action_group_name
  resource_group_name = data.azurerm_resource_group.main.name
}

module "monitor_activity_log_alert" {
  source = "../../../modules/azure/monitor-activity-log-alert"

  monitor_activity_log_alert             = var.monitor_activity_log_alert
  resource_group_name                    = data.azurerm_resource_group.main.name
  monitor_activity_log_alert_scope       = ["/subscriptions/${data.azurerm_subscription.current.subscription_id}"]
  monitor_activity_log_alert_description = var.monitor_activity_log_alert_description
  monitor_activity_log_alert_enabled     = var.monitor_activity_log_alert_enabled
  criteria_category                      = var.criteria_category
  service_health_events                  = var.service_health_events
  service_health_locations               = var.service_health_locations
  service_health_services                = var.service_health_services
  action_group_id                        = data.azurerm_monitor_action_group.main.id
  tag_environment                        = var.tag_environment
  tag_billing_group                      = var.tag_billing_group
}

用于创建资源的模块变量文件

variable "monitor_activity_log_alert" {
  type        = string
  description = "The name of the activity log alert"
  default     = "my-service-health-alert-global"
}

variable "resource_group_name" {
  type        = string
  description = "The name of the resource group in which to create the activity log alert instance."
  default     = "MyGlobalRG"
}

variable "monitor_action_group_name" {
  type        = string
  description = "The name of the Action Group can be sourced from the azurerm_monitor_action_group resource"
  default     = "my-global-mag"
}

variable "monitor_activity_log_alert_description" {
  type        = string
  description = "The description of this activity log alert."
  default     = "This activity log alert is to monitor the health of all services in the Global and US West 2 regions"
}

variable "monitor_activity_log_alert_enabled" {
  type        = bool
  description = "Should this Activity Log Alert be enabled? Defaults to true."
  default     = true
}

variable "criteria_category" {
  type        = string
  description = "The category of the operation. Possible values are Administrative, Autoscale, Policy, Recommendation, ResourceHealth, Security and ServiceHealth."
  default     = "ServiceHealth"
}

variable "service_health_events" {
  type        = list(string)
  description = "Events this alert will monitor Possible values are Incident, Maintenance, Informational, ActionRequired and Security. Defaults to all Events or Set to null to select all Events"
  default     = null
}

variable "service_health_locations" {
  type        = list(string)
  description = "Locations this alert will monitor. For example, West Europe. Defaults to Global."
  default     = ["global", "westus2"]
}

variable "service_health_services" {
  type        = list(string)
  description = "Services this alert will monitor. For example, Activity Logs & Alerts, Action Groups. Defaults to all Services or Set to null to select all Services."
  default     = null
}

variable "tag_environment" {
  type        = string
  description = "A mapping of tags which should be assigned to the resource."
  default     = "global"
}

就是这样

【讨论】:

    猜你喜欢
    • 2019-03-04
    • 2021-12-08
    • 2021-03-31
    • 1970-01-01
    • 2019-05-15
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    • 2018-12-30
    相关资源
    最近更新 更多