# DRD Alerts from subscription filter
resource "aws_sns_topic" "digsys_eom_drd_alert" {
  #checkov:skip=CKV_AWS_26: Ensure all data stored in the SNS topic is encrypted
  name = "digsys-eom-drd-alert"
}

resource "aws_sns_topic_subscription" "digsys_eom_drd_alert_ivan_email" {
  topic_arn = aws_sns_topic.digsys_eom_drd_alert.arn
  protocol  = "email"
  endpoint  = "ivan.larionov.intimetechnologies@sonymusic.com"
}

resource "aws_lambda_permission" "digsys_eom_drd_allow_cloudwatch_logs_send" {
  statement_id  = "AllowExecutionFromDrdCloudWatchLogs"
  action        = "lambda:InvokeFunction"
  function_name = "digsys-eom-cloudwatch-logs-alert"
  principal     = "logs.amazonaws.com"
  source_arn    = "arn:aws:logs:${var.region}:${var.account_id}:log-group:/aws/containerinsights/${data.aws_eks_cluster.digsys-eom-eks.name}/application/${var.digsys-eom-drd-eks-namespace}:*"
}

resource "aws_cloudwatch_log_subscription_filter" "digsys_eom_drd_log_subscription_filter" {
  name            = "digsys-eom-drd-log-subscription-filter"
  log_group_name  = aws_cloudwatch_log_group.digsys_eom_drd.name
  filter_pattern  = "{ $.log_processed.level = WARN || $.log_processed.level = WARNING || $.log_processed.level = ERROR }"
  destination_arn = "arn:aws:lambda:${var.region}:${var.account_id}:function:digsys-eom-cloudwatch-logs-alert"
}


resource "aws_cloudwatch_log_group" "digsys_eom_drd" {
  #checkov:skip=CKV_AWS_338: Ensure CloudWatch log groups retains logs for at least 1 year
  name = "/aws/containerinsights/${data.aws_eks_cluster.digsys-eom-eks.name}/application/${var.digsys-eom-drd-eks-namespace}"
  retention_in_days = 7
}

import {
  to = aws_cloudwatch_log_group.digsys_eom_drd
  id = "/aws/containerinsights/digsysd3-eks/application/drd"
}