resource "aws_ecs_service" "aomap_txmgr_usm_service" {
  name        = "aomap-txmgr-usm-service"
  cluster     = aws_ecs_cluster.aomap_txmgr_usm_ecs.id
  launch_type = "EC2"
  depends_on  = [aws_instance.aomap-txmgr-usm-ecs-01]

  # Keep this aligned with the current running service before first apply.
  desired_count = 1

  # CI/CD updates this out-of-band; Terraform should not roll it back.
  task_definition = "arn:aws:ecs:eu-central-1:613871678587:task-definition/aomap-txmgr-usm:6"

  enable_ecs_managed_tags            = true
  wait_for_steady_state              = false
  deployment_maximum_percent         = 200
  deployment_minimum_healthy_percent = 100
  health_check_grace_period_seconds  = 0
  enable_execute_command             = false
  propagate_tags                     = "NONE"

  deployment_circuit_breaker {
    enable   = true
    rollback = true
  }

  ordered_placement_strategy {
    type  = "spread"
    field = "attribute:ecs.availability-zone"
  }

  ordered_placement_strategy {
    type  = "spread"
    field = "instanceId"
  }

  lifecycle {
    ignore_changes = [
      task_definition,
      desired_count,
    ]
  }
}

resource "aws_ecs_service" "aomap_txmgr_kafka_consumer_service" {
  name        = "aomap-txmgr-kafka-consumer-service"
  cluster     = aws_ecs_cluster.aomap_txmgr_kafka_consumer.id
  launch_type = "EC2"
  depends_on  = [aws_instance.aomap-txmgr-kafka-consumer-ecs-01]

  # Keep this aligned with the current running service before first apply.
  desired_count = 1

  # CI/CD updates this out-of-band; Terraform should not roll it back.
  task_definition = "arn:aws:ecs:eu-central-1:613871678587:task-definition/aomap-txmgr-kafka-consumer:18"

  enable_ecs_managed_tags            = true
  wait_for_steady_state              = false
  deployment_maximum_percent         = 200
  deployment_minimum_healthy_percent = 100
  health_check_grace_period_seconds  = 0
  enable_execute_command             = false
  propagate_tags                     = "NONE"

  deployment_circuit_breaker {
    enable   = true
    rollback = true
  }

  ordered_placement_strategy {
    type  = "spread"
    field = "attribute:ecs.availability-zone"
  }

  ordered_placement_strategy {
    type  = "spread"
    field = "instanceId"
  }

  lifecycle {
    ignore_changes = [
      task_definition,
      desired_count,
    ]
  }
}

resource "aws_ecs_service" "aomap_txmgr" {
  # checkov:skip=CKV_AWS_65: Ensure container insights are enabled on ECS cluster
  # checkov:skip=CKV_AWS_224: Ensure ECS Cluster logging is enabled and client to container communication uses CMK
  name                = "aomap-txmgr"
  cluster             = aws_ecs_cluster.aomap_txmgr_ecs.id
  launch_type         = "EC2"
  scheduling_strategy = "DAEMON"
  depends_on = [
    aws_instance.aomap-txmgr-ecs-01,
    aws_instance.aomap-txmgr-ecs-02,
    aws_instance.aomap-txmgr-ecs-03,
    aws_instance.aomap-txmgr-ecs-04,
  ]

  # Keep this aligned with the current running service before first apply.
  desired_count = 4

  # CI/CD updates this out-of-band; Terraform should not roll it back.
  task_definition = "arn:aws:ecs:eu-central-1:613871678587:task-definition/aomap-txmgr-taskdef:57"

  enable_ecs_managed_tags            = true
  deployment_maximum_percent         = 100
  deployment_minimum_healthy_percent = 0
  health_check_grace_period_seconds  = 0
  enable_execute_command             = false
  propagate_tags                     = "NONE"

  deployment_controller {
    type = "ECS"
  }

  lifecycle {
    ignore_changes = [
      task_definition,
      desired_count,
    ]
  }
}