module "sentry_ec2" {
  source = "../../../modules/ec2/ec2"

  ami                  = "ami-03588e6b2c2023413" // data.aws_ami.amazon_linux_2.id
  key_name             = var.key_pair_name
  instance_type        = "t3.medium"
  subnet_ids           = data.aws_subnets.public_subnets.ids
  use_public_ip        = true
  root_volume_size     = 120
  root_volume_type     = "gp3"
  name_prefix          = "${local.name_prefix}-sentry"
  iam_instance_profile = module.sentry_ec2_role.profile.name
  encrypted            = true
  kms_key_id           = data.aws_kms_alias.default_ebs_key.target_key_arn

  enable_alarm = true
  alarm_name   = "${local.name_prefix}-sentry_ec2_status"

  alarm_actions = [
    data.aws_sns_topic.devops_team_notifications.arn,
  ]
  ok_actions = [
    data.aws_sns_topic.devops_team_notifications.arn,
  ]
  insufficient_data_actions = [
    data.aws_sns_topic.devops_team_notifications.arn,
  ]

  security_groups = [
    aws_security_group.sg_sentry.id,
    data.aws_security_group.remote_access.id,
  ]

  instance_tags = {
    os_platform     = "linux",
    os_distribution = "amazon",
    splunk          = "true",
  }

  common_tags = local.sentry_tags
}

/*
module "sentry_tg" {
  source               = "../../modules/ec2/tg"
  name_prefix          = local.name_prefix
  common_tags          = local.sentry_tags
  tg_name              = "sentry"
  tg_vpc_id            = module.main_vpc.vpc_id
  tg_health_check_path = "/_health/"
  tg_targets           = module.sentry_ec2.ec2_ids[0]
  tg_port              = 9000
}

resource "aws_alb_listener_rule" "sentry_forward" {
  listener_arn = module.main_alb_listener_https.alb_listener["arn"]

  action {
    type             = "forward"
    target_group_arn = module.sentry_tg.tg.arn
  }

  condition {
    host_header {
      values = [local.sentry_domain_name]
    }
  }
}

resource "aws_alb_listener_rule" "sentry_redirect" {
  listener_arn = module.main_alb_listener_http.alb_listener["arn"]

  action {
    type = "redirect"
    redirect {
      host        = local.sentry_domain_name
      port        = "443"
      protocol    = "HTTPS"
      status_code = "HTTP_302"
    }
  }

  condition {
    host_header {
      values = [local.sentry_domain_name]
    }
  }
}
*/

resource "aws_cloudwatch_metric_alarm" "sentry_db_free_disk_space" {
  alarm_name          = "${local.name_prefix}-sentry_db_free_storage"
  alarm_description   = "Amount of free storage space is lower than 10 GiB."
  comparison_operator = "LessThanThreshold"
  evaluation_periods  = 2
  metric_name         = "FreeStorageSpace"
  namespace           = "AWS/RDS"
  statistic           = "Average"
  period              = 3600
  threshold           = 10 * 1024 * 1024 * 1024 // 10 GiB
  actions_enabled     = true
  alarm_actions = [
    data.aws_sns_topic.devops_team_notifications.arn,
  ]
  ok_actions = [
    data.aws_sns_topic.devops_team_notifications.arn,
  ]

  dimensions = {
    DBInstanceIdentifier = aws_db_instance.sentry_db.identifier
  }

  tags = local.common_tags
}

resource "aws_cloudwatch_metric_alarm" "sentry_ec2_nvme0n1p1_disk_usage" {
  alarm_name          = "${local.name_prefix}-sentry_nvme0n1p1_disk_usage"
  alarm_description   = "Percent of used disk space is greater than 80 percent."
  comparison_operator = "GreaterThanThreshold"
  evaluation_periods  = 2
  metric_name         = "disk_used_percent"
  namespace           = "CWAgent"
  statistic           = "Average"
  period              = 3600
  threshold           = 80
  actions_enabled     = true
  alarm_actions = [
    data.aws_sns_topic.devops_team_notifications.arn,
  ]

  ok_actions = [
    data.aws_sns_topic.devops_team_notifications.arn,
  ]

  dimensions = {
    InstanceId   = module.sentry_ec2.ec2.0.id
    ImageId      = module.sentry_ec2.ec2.0.ami
    InstanceType = module.sentry_ec2.ec2.0.instance_type
    device       = "nvme0n1p1"
    fstype       = "xfs"
    path         = "/"
  }
}


