resource "aws_ecs_service" "aomas_txmgr_usm_service" {
  name        = "aomas-txmgr-usm-service"
  cluster     = aws_ecs_cluster.aomas_txmgr_usm_ecs.id
  launch_type = "EC2"
  depends_on  = [aws_instance.aomas-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:969009837416:task-definition/aomas-txmgr-usm:9"

  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" "aomas_txmgr_kafka_consumer_service" {
  name        = "aomas-txmgr-kafka-consumer-service"
  cluster     = aws_ecs_cluster.aomas_txmgr_kafka_consumer.id
  launch_type = "EC2"
  depends_on  = [aws_instance.aomas-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:969009837416:task-definition/aomas-txmgr-kafka-consumer:51"

  enable_ecs_managed_tags = true
  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 = false
  }

  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" "aomas_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        = "aomas-txmgr"
  cluster     = aws_ecs_cluster.aomas_txmgr_ecs.id
  launch_type = "EC2"
  scheduling_strategy = "DAEMON"
  depends_on  = [
    aws_instance.aomas-txmgr05,
    aws_instance.aomas-txmgr06,
    aws_instance.aomas-txmgr07,
    aws_instance.aomas-txmgr08,
  ]

  # 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:969009837416:task-definition/aomas-txmgr-taskdef:402"

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

  deployment_controller {
    type = "ECS"
  }

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