Terraform 基于Cloudwatch规则创建PagerDuty报警的最佳方法

Terraform 基于Cloudwatch规则创建PagerDuty报警的最佳方法,terraform,amazon-cloudwatch,pagerduty,Terraform,Amazon Cloudwatch,Pagerduty,PagerDuty(PD)与Cloudwatch(CW)集成,每当触发CW报警时,我都会使用它进行寻呼: 如果触发了CW规则,我希望被呼叫。看起来我可以使用PD全局事件路由,然后配置CW输入以发送PD全局事件端点期望的响应。但我喜欢CW警报发布到SNS主题的方式,我所要做的就是将SNS主题消息转发给PD,所以如果CW规则也有类似的内容就好了 事实证明,我可以使用TriggeredRules度量从规则创建CW警报。然后我可以使用现有的PagerDuty CW集成。这是我写的地形代码: data "

PagerDuty(PD)与Cloudwatch(CW)集成,每当触发CW报警时,我都会使用它进行寻呼:


如果触发了CW规则,我希望被呼叫。看起来我可以使用PD全局事件路由,然后配置CW输入以发送PD全局事件端点期望的响应。但我喜欢CW警报发布到SNS主题的方式,我所要做的就是将SNS主题消息转发给PD,所以如果CW规则也有类似的内容就好了

事实证明,我可以使用
TriggeredRules
度量从规则创建CW警报。然后我可以使用现有的PagerDuty CW集成。这是我写的地形代码:

data "template_file" "ecs_task_stopped" {
  template = <<EOF
{
  "source": ["aws.ecs"],
  "detail-type": ["ECS Task State Change"],
  "detail": {
    "clusterArn": ["arn:aws:ecs:$${aws_region}:$${account_id}:cluster/$${cluster}"],
    "desiredStatus": ["Running"],
    "lastStatus": ["STOPPED"]
  }
}
EOF

  vars {
    account_id = "${data.aws_caller_identity.current.account_id}"
    cluster    = "${var.ecs_cluster_name}"
    aws_region = "${data.aws_region.current.name}"
  }
}

resource "aws_cloudwatch_event_rule" "ecs_task_stopped" {
  count         = "${var.should_create == "true" ? 1 : 0}"
  name          = "${var.env}_${var.ecs_cluster_name}_task_stopped"
  description   = "${var.env}_${var.ecs_cluster_name} Essential container in task exited"
  event_pattern = "${data.template_file.ecs_task_stopped.rendered}"
}

resource "aws_cloudwatch_metric_alarm" "alarm_task_stopped_rule_triggered" {
  count               = "${var.should_create == "true" ? 1 : 0}"
  alarm_name          = "${var.ecs_cluster_name}-task-stopped"
  comparison_operator = "GreaterThanOrEqualToThreshold"
  evaluation_periods  = "1"
  datapoints_to_alarm = "1"
  metric_name         = "TriggeredRules"
  namespace           = "AWS/Events"
  period              = "60"
  statistic           = "Maximum"
  threshold           = "1"
  alarm_description   = "Essential container in ${var.ecs_cluster_name} task exited"
  alarm_actions       = ["${var.cw_sns_topic_id}"]
  ok_actions          = ["${var.cw_sns_topic_id}"]

  dimensions {
    RuleName = "${aws_cloudwatch_event_rule.ecs_task_stopped.name}"
  }
}
数据“模板文件”“ecs任务已停止”{
模板=