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

  name_prefix          = "${local.name_prefix}-utility-general"
  instances_count      = 1
  ami                  = "ami-0be0e902919675894"
  instance_type        = "r5.xlarge"
  use_public_ip        = false
  key_name             = var.key_pair_name
  security_groups      = [module.main_sg_map.win_batch_id, module.main_sg_map.management_rdp_id]
  subnet_ids           = [module.main_private_subnets.subnet_ids[0]]
  iam_instance_profile = module.main_iam_profiles.profile_ec2.name
  root_volume_size     = 250
  root_volume_type     = "gp3"

  enable_alarm              = true
  alarm_name                = "${local.name_prefix}-utility-general-status-check-failed-any"
  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]

  instance_tags = {
    os_platform = "windows",
  }

  common_tags = merge(
    local.common_tags,
    {
      project                  = "ETL",
      service                  = "EC2",
      plat_env_project_service = "${local.aggregated_tag}_ETL_EC2",
    }
  )
}

# Amazon Linux with Couchbase 4.1.2 inside a docker
module "ec2_couchbase_docker_1" {
  source = "../../../modules/ec2/ec2"

  name_prefix            = "${local.name_prefix}-couchbase-docker-1"
  instances_count        = 1
  ami                    = "ami-0cff7528ff583bf9a"
  instance_type          = "t3.medium"
  use_public_ip          = false
  key_name               = var.key_pair_name
  termination_protection = true
  security_groups = [
    module.main_sg_map.couchbase_id,
    module.main_sg_map.management_ssh_id,
    module.main_sg_map.vpn_http_id,
  ]
  subnet_ids           = [module.main_private_subnets.subnet_ids[0]]
  iam_instance_profile = module.main_iam_profiles.profile_ec2.name
  root_volume_size     = 75
  root_volume_type     = "gp3"
  encrypted            = true
  kms_key_id           = data.aws_kms_alias.default_ebs_key.target_key_arn

  enable_alarm = true
  alarm_name   = "${local.name_prefix}-couchbase-docker-1-status-check-failed-any"

  common_tags = merge(
    local.common_tags,
    {
      project                  = "Portal",
      service                  = "EC2",
      plat_env_project_service = "${local.aggregated_tag}_PRL_EC2",
      os_platform              = "linux",
      os_distribution          = "amazon",
      splunk                   = "true",
      role                     = "couchbase",
    }
  )
}

# Amazon Linux with Couchbase 4.1.2 inside a docker
module "ec2_couchbase_docker_2" {
  source = "../../../modules/ec2/ec2"

  name_prefix            = "${local.name_prefix}-couchbase-docker-2"
  instances_count        = 1
  ami                    = "ami-0cff7528ff583bf9a"
  instance_type          = "t3.medium"
  use_public_ip          = false
  key_name               = var.key_pair_name
  termination_protection = true
  security_groups = [
    module.main_sg_map.couchbase_id,
    module.main_sg_map.management_ssh_id,
    module.main_sg_map.vpn_http_id,
  ]
  subnet_ids           = [module.main_private_subnets.subnet_ids[1]]
  iam_instance_profile = module.main_iam_profiles.profile_ec2.name
  root_volume_size     = 75
  root_volume_type     = "gp3"
  encrypted            = true
  kms_key_id           = data.aws_kms_alias.default_ebs_key.target_key_arn

  enable_alarm = true
  alarm_name   = "${local.name_prefix}-couchbase-docker-2-status-check-failed-any"

  common_tags = merge(
    local.common_tags,
    {
      project                  = "Portal",
      service                  = "EC2",
      plat_env_project_service = "${local.aggregated_tag}_PRL_EC2",
      os_platform              = "linux",
      os_distribution          = "amazon",
      splunk                   = "true",
      role                     = "couchbase",
    }
  )
}

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

  name_prefix            = "${local.name_prefix}-web-win2022-1"
  instances_count        = 1
  ami                    = "ami-0fc682b2a42e57ca2" // Microsoft Windows Server 2022 Full Locale English AMI provided by Amazon
  instance_type          = "c5.xlarge"
  use_public_ip          = false
  key_name               = var.key_pair_name
  termination_protection = true

  security_groups = [
    module.main_sg_map.win_ws_id,
    module.main_sg_map.couchbase_id,
    module.main_sg_map.management_rdp_id,
    module.main_sg_map.web_debug_id_80,
    module.main_sg_map.web_debug_id_443,
  ]
  subnet_ids           = [module.main_private_subnets.subnet_ids[0]]
  iam_instance_profile = module.main_iam_profiles.profile_ec2.name
  root_volume_size     = 100
  root_volume_type     = "gp3"
  encrypted            = true
  kms_key_id           = data.aws_kms_alias.default_ebs_key.target_key_arn

  enable_alarm = true
  alarm_name   = "${local.name_prefix}-web-win2022-1-status-check-failed-any"

  common_tags = merge(
    local.common_tags,
    {
      project                  = "ETL",
      service                  = "EC2",
      plat_env_project_service = "${local.aggregated_tag}_ETL_EC2",
      os_platform              = "windows",
      role                     = "Apollo .NET API",
    }
  )
}

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

  name_prefix            = "${local.name_prefix}-web-win2022-2"
  instances_count        = 1
  ami                    = "ami-0fc682b2a42e57ca2" // Microsoft Windows Server 2022 Full Locale English AMI provided by Amazon
  instance_type          = "c5.xlarge"
  use_public_ip          = false
  key_name               = var.key_pair_name
  termination_protection = true

  security_groups = [
    module.main_sg_map.win_ws_id,
    module.main_sg_map.couchbase_id,
    module.main_sg_map.management_rdp_id,
    module.main_sg_map.web_debug_id_80,
    module.main_sg_map.web_debug_id_443,
  ]
  subnet_ids           = [module.main_private_subnets.subnet_ids[1]]
  iam_instance_profile = module.main_iam_profiles.profile_ec2.name
  root_volume_size     = 100
  root_volume_type     = "gp3"
  encrypted            = true
  kms_key_id           = data.aws_kms_alias.default_ebs_key.target_key_arn

  enable_alarm = true
  alarm_name   = "${local.name_prefix}-web-win2022-2-status-check-failed-any"

  common_tags = merge(
    local.common_tags,
    {
      project                  = "ETL",
      service                  = "EC2",
      plat_env_project_service = "${local.aggregated_tag}_ETL_EC2",
      os_platform              = "windows",
      role                     = "Apollo .NET API",
    }
  )
}

// Temporary IAM role and instance profile for backfilling Spotify Consumer Analytics data
data "aws_iam_policy_document" "assume_ec2" {
  statement {
    actions = ["sts:AssumeRole"]

    principals {
      type        = "Service"
      identifiers = ["ec2.amazonaws.com"]
    }
  }
}

data "aws_iam_policy" "s3_aggregated_stream_reports_rw" {
  name = "${local.name_prefix}-s3_aggregated_stream_reports_rw"
}

data "aws_iam_policy" "s3_delphi_dataarchive_ro" {
  name = "${local.name_prefix}-s3_delphi_dataarchive_ro"
}

resource "aws_iam_role" "spotifyconsumeranalytics_iam_role" {
  name               = "${local.name_prefix}-spotifyconsumeranalytics-backfill-role"
  assume_role_policy = data.aws_iam_policy_document.assume_ec2.json
  path               = "/"

  tags = merge(
    local.common_tags,
    {
      project                  = "ETL",
      service                  = "IAM",
      plat_env_project_service = "${local.aggregated_tag}_ETL_IAM",
      role                     = "spotifyconsumeranalytics-backfill",
      ticket                   = "SYS-28154",
    }
  )
}

resource "aws_iam_role_policy_attachment" "spotifyconsumeranalytics_s3_aggregated_stream_reports_rw_attachment" {
  role       = aws_iam_role.spotifyconsumeranalytics_iam_role.name
  policy_arn = data.aws_iam_policy.s3_aggregated_stream_reports_rw.arn
}

resource "aws_iam_role_policy_attachment" "spotifyconsumeranalytics_s3_delphi_dataarchive_ro_attachment" {
  role       = aws_iam_role.spotifyconsumeranalytics_iam_role.name
  policy_arn = data.aws_iam_policy.s3_delphi_dataarchive_ro.arn
}

resource "aws_iam_instance_profile" "spotifyconsumeranalytics_instance_profile" {
  name = "${local.name_prefix}-spotifyconsumeranalytics-backfill-profile"
  role = aws_iam_role.spotifyconsumeranalytics_iam_role.name

  tags = merge(
    local.common_tags,
    {
      project                  = "ETL",
      service                  = "IAM",
      plat_env_project_service = "${local.aggregated_tag}_ETL_IAM",
      role                     = "spotifyconsumeranalytics-backfill",
      ticket                   = "SYS-28154",
    }
  )
}

// Temporary instance for backfilling Spotify Consumer Analytics data
// we need at least 256 GiB RAM to run the backfill process
// must be x86_64 architecture
module "temp_instance_to_backfill_spotify_data" {
  source = "../../../modules/ec2/ec2"

  name_prefix            = "${local.name_prefix}-spotifyconsumeranalytics-sony-backfill"
  instances_count        = 1
  ami                    = "ami-068c0051b15cdb816" // Amazon Linux 2 AMI (HVM), SSD Volume Type - ami-068c0051b15cdb816 (us-east-1)
  instance_type          = "r5a.8xlarge" // 32 vCPU, 256 GiB RAM
  use_public_ip          = false
  key_name               = var.key_pair_name
  termination_protection = true
  security_groups = [
    module.main_sg_map.management_ssh_id,
  ]
  subnet_ids           = [module.main_private_subnets.subnet_ids[0]]
  iam_instance_profile = aws_iam_instance_profile.spotifyconsumeranalytics_instance_profile.name
  root_volume_size     = 750
  root_volume_type     = "gp3"
  encrypted            = true
  kms_key_id           = data.aws_kms_alias.default_ebs_key.target_key_arn

  common_tags = merge(
    local.common_tags,
    {
      project                  = "ETL",
      service                  = "EC2",
      plat_env_project_service = "${local.aggregated_tag}_ETL_EC2",
      os_platform              = "linux",
      os_distribution          = "amazon",
      splunk                   = "true",
      role                     = "spotifyconsumeranalytics",
      ticket                   = "SYS-28154",
    }
  )
}
