resource "datadog_monitor" "ec2_status_check_monitor" {
  name    = "${var.environment}-${var.service_name} instance {{instance_id.name}} state check failed"
  type    = "query alert"
  message = <<EOF
{{#is_alert}} {{instance_id.name}} has failed status check {{/is_alert}}
{{#is_alert_recovery}} {{instance_id.name}} has recovered status check {{/is_alert_recovery}}

${var.slack_channel}
EOF

  query = "max(last_5m):min:aws.ec2.status_check_failed{alias:${var.account_alias}} by {instance_id} >= 1"

  monitor_thresholds {
    critical          = 1
    critical_recovery = 0
  }

  evaluation_delay = 900 // minimum recommended value for AWS metrics 900

  notify_no_data    = false
  renotify_interval = 300
  restricted_roles  = [data.datadog_role.datadog_admin_role.id]

  notify_audit = false
  timeout_h    = 0
  include_tags = true

  tags = local.common_tags
}
