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

  name_prefix          = "${local.name_prefix}-jenkins-master"
  instances_count      = 1
  ami                  = "ami-0eb22fc156d155d30"
  instance_type        = "t3.medium"
  use_public_ip        = true
  use_eip              = true
  key_name             = var.jenkins_key_pair_name
  security_groups      = [module.jenkins_sg_map.remote_ssh_id, module.jenkins_sg_map.jenkins_master_id]
  subnet_ids           = module.jenkins_public_subnets.subnet_ids
  iam_instance_profile = aws_iam_instance_profile.jenkins_ec2_server.name
  root_volume_size     = 180
  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}-jenkins-master-status-check"

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

  common_tags = {
    service                  = "EC2",
    plat_env_project_service = "${local.aggregated_tag}_EC2",
  }
}

# Load balancing rules
module "tg_jenkins_master" {
  source = "../../../modules/ec2/tg"

  name_prefix          = "${local.name_prefix}-jenkins"
  tg_name              = "master"
  tg_vpc_id            = module.jenkins_vpc.vpc_id
  tg_port              = 8080
  tg_health_check_path = "/login"
  tg_targets           = module.ec2_jenkins_master.ec2[*].id

  tg_health_check_interval            = 61
  tg_health_check_timeout             = 60
  tg_health_check_unhealthy_threshold = 10

  common_tags = {
    service                  = "EC2",
    plat_env_project_service = "${local.aggregated_tag}_EC2",
  }
}
