terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "prod/datadog/events/aws_health/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

resource "datadog_monitor" "aws_health_events" {
  groupby_simple_monitor = false
  include_tags           = false
  on_missing_data        = "resolve"
  require_full_window    = false
  monitor_thresholds {
    critical = 0
  }
  name    = "[{{event.attributes.priority}}]- {{event.title}} - {{event.attributes.status}}"
  type    = "event-v2 alert"
  tags    = ["team:devops", "application_family:devops"]
  query   = <<EOT
events("source:amazon_health").rollup("count").by("@evt.id").last("5m") > 0
EOT
  message = <<EOT
{{#is_alert}}
  {{#is_match "event.tags.aws_account" "437795906767" "734458723522" "086679231553" "079637511089" "682033496752" "313114233826" "703740486246"}}
    {{!-- handle notifications for the production, networking, shared, backup, disaster recovery, business-intelligence, crm --}}
    @slack-devops-internal-alerts
  {{/is_match}}

  {{#is_match "event.tags.aws_account" "103233932089"}}
    {{!-- handle notifications for the development account --}}
    @slack-devops-internal-alerts
  {{/is_match}}

  {{#is_match "event.tags.aws_account" "290365479392"}}
    {{!-- handle notifications for the RoyaltyShare account --}}
  {{/is_match}}

  {{#is_match "event.tags.aws_account" "619719722105" "926734670777"}}
    {{!-- handle notifications for the SongWhip accounts --}}
    @slack-songwhip-alerts
  {{/is_match}}

  {{#is_match "event.tags.aws_account" "591204808501" "031099521156"}}
    {{!-- handle notifications for the Permissions Platform accounts --}}
    @slack-permissions-platform-alerts
  {{/is_match}}

  {{#is_match "event.tags.aws_account" "989790945997" "375914681009"}}
    {{!-- handle notifications for the Accounting accounts --}}
    @slack-abacus-monitoring
  {{/is_match}}

  {{#is_match "event.tags.aws_account" "285943604611" "737325105821"}}
    {{!-- handle notifications for the Fansifter accounts --}}
    @slack-fansifter-alerts
  {{/is_match}}

  {{#is_match "event.tags.aws_account" "725764004555" "801577982711"}}
    {{!-- handle notifications for the Apollo accounts --}}
    @slack-apollo-datadog-alerts
  {{/is_match}}

  {{#is_match "event.tags.aws_account" "745243859288" "807702934854"}}
    {{!-- handle notifications for the Databricks accounts --}}
    @slack-delphi-infra-alerts
  {{/is_match}}

  {{#is_match "event.tags.aws_account" "475275892927" "323555055331"}}
    {{!-- handle notifications for the Delphi accounts --}}
    @slack-delphi-infra-alerts
  {{/is_match}}

  {{#is_match "event.tags.aws_account" "582412345909" "885200931856"}}
    {{!-- handle notifications for the Atlas accounts --}}
    @slack-core-datadog-alerts
  {{/is_match}}

  {{#is_match "event.tags.aws_account" "322697034778"}}
    {{!-- handle notifications for the Whitelist account --}}
    @slack-devops-internal-alerts
  {{/is_match}}

  {{#is_match "event.tags.aws_account" "814622134907" "009424872958"}}
    {{!-- handle notifications for the business-solutions accounts --}}
    @slack-devops-internal-alerts
  {{/is_match}}

  {{#is_match "event.tags.aws_account" "311141540202" "710271914708"}}
    {{!-- handle notifications for the supply-chain accounts --}}
    @slack-devops-internal-alerts
  {{/is_match}}

  {{#is_match "event.tags.aws_account" "699475937692"}}
    {{!-- handle notifications for the test-automation account --}}
    @slack-devops-internal-alerts
  {{/is_match}}

  {{#is_match "event.tags.aws_account" "635057242373" "588827373449"}}
    {{!-- handle notifications for the mobile-code-push accounts --}}
    @slack-devops-internal-alerts
  {{/is_match}}
{{/is_alert}}

### Event Details

A new AWS Health event has been detected in account {{event.tags.aws_account}} ({{event.tags.aws_account_alias}}) for service: {{event.tags.service}}.
EOT
}
