# dsms-mi-test-ecs-service.tf

############################
# LOG GROUPS
############################
resource "aws_cloudwatch_log_group" "dsms-mi-test" {
  #checkov:skip=CKV_AWS_338: Ensure CloudWatch log groups retains logs for at least 1 year
  name              = "/ecs/dsms-mi-test"
  retention_in_days = 7
}

############################
# TASK DEFINITIONS
############################
resource "aws_ecs_task_definition" "dsms-mi-test" {
  family                   = "${lower(var.vpc_name)}-dsms-mi-test-td"
  network_mode             = "awsvpc"
  requires_compatibilities = ["FARGATE"]
  cpu                      = "512"
  memory                   = "1024"
  execution_role_arn       = "arn:aws:iam::718729873097:role/ecsTaskExecutionRole"
  task_role_arn            = "arn:aws:iam::718729873097:role/dsms-task-role"

  container_definitions = jsonencode([
    {
      name      = "dsms-mi"
      image     = "718729873097.dkr.ecr.eu-central-1.amazonaws.com/dsms/mi:latest"
      essential = true

      portMappings = [{
        containerPort = 8550
        hostPort      = 8550
        protocol      = "tcp"
      }]

      healthCheck = {
        command     = ["CMD-SHELL", "curl -f http://localhost:8550/actuator/health || exit 1"]
        interval    = 30
        timeout     = 5
        retries     = 3
        startPeriod = 60
      }

      environment = [
        { name = "SPRING_PROFILES_ACTIVE", value = "test" },
        { name = "JAVA_TOOL_OPTIONS", value = "-XX:+UseContainerSupport" }
      ]
      
      secrets = [
      {
        name      = "DB_PASSWORD"
        valueFrom = "arn:aws:secretsmanager:eu-central-1:718729873097:secret:dsms-test/config-gsvBqh:DB_PASSWORD::"
      },
      {
        name      = "EUREKA_PASSWORD"
        valueFrom = "arn:aws:secretsmanager:eu-central-1:718729873097:secret:dsms-test/config-gsvBqh:EUREKA_PASSWORD::"
      }
    ]

      logConfiguration = {
        logDriver = "awslogs"
        options = {
          awslogs-region        = var.region
          awslogs-group         = "/ecs/dsms-mi-test"
          awslogs-stream-prefix = "ecs"
        }
      }
    }
  ])
}


############################
# SERVICES
############################
resource "aws_ecs_service" "dsms-mi-test" {
  name                  = "${lower(var.vpc_name)}-dsms-mi-test"
  cluster               = "arn:aws:ecs:eu-central-1:718729873097:cluster/dsms-cluster"
  task_definition       = aws_ecs_task_definition.dsms-mi-test.arn
  desired_count         = 1
  launch_type           = "FARGATE"
  platform_version      = "LATEST"
  enable_execute_command = true
  deployment_minimum_healthy_percent = 50
  deployment_maximum_percent         = 200

  network_configuration {
    subnets         = [aws_subnet.DIGSYS-D3-IAPP-1A.id, aws_subnet.DIGSYS-D3-IAPP-1B.id]
    security_groups = [aws_security_group.DIGSYS-D3-DSMS-MI-SG.id]
    assign_public_ip = false
  }



}


