有没有办法使用terraform为已部署资源的正常运行时间检查创建GCP警报策略,并过滤
metric.label.check_id
的值?文档提供的示例仅显示针对度量标准而非针对已部署资源的正常运行时间检查的警报策略,因此我不确定是否可以使用terraform实现。metric.label.check_id
的值?文档提供的示例仅显示针对度量标准而非针对已部署资源的正常运行时间检查的警报策略,因此我不确定是否可以使用terraform实现。我已经找到了一个解决方案,它在我的情况下可以正常工作。
我创建了两个单独的 Terraform 模块,分别用于创建运行时间检查和运行时间检查警报。
Terraform 运行时间检查模块的外观如下:
resource "google_monitoring_uptime_check_config" "uptime-check" {
project = var.project_id
display_name = var.display_name
timeout = "10s"
period = "60s"
http_check {
path = var.path
port = var.port
use_ssl = true
validate_ssl = true
}
monitored_resource {
type = "uptime_url"
labels = {
host = var.hostname,
project_id = var.project_id
}
}
content_matchers {
content = "\"status\":\"UP\""
}
}
那么对于该模块的 outputs.tf
,我有以下内容:
output "uptime_check_id" {
value = google_monitoring_uptime_check_config.uptime-check.uptime_check_id
}
然后在告警模块中,我遵循terraform文档的步骤,但对其进行了修改,使其看起来像这样的代码:
module "medallies-common-alerts" {
source = "./modules/alerts"
project_id = var.project_id
uptime_check_depends_on = [module.uptime-check]
check_id = module.uptime-check.uptime_check_id
}
...
resource "google_monitoring_alert_policy" "alert_policy_uptime_check" {
project = var.project_id
enabled = true
depends_on = [var.uptime_check_depends_on]
....
condition_threshold {
filter = format("metric.type=\"monitoring.googleapis.com/uptime_check/check_passed\" AND metric.label.\"check_id\"=\"%s\" AND resource.type=\"uptime_url\"",var.check_id)
duration = "300s"
comparison = "COMPARISON_GT"
threshold_value = "1"
trigger {
count = 1
}
...
}