resource "aws_ecs_task_definition" "this" {
  family                   = var.family
  cpu                      = tostring(var.cpu)
  memory                   = tostring(var.memory)
  requires_compatibilities = local.requires_compatibilities
  network_mode             = local.network_mode
  container_definitions    = local.container_definitions
}

resource "aws_ecs_service" "this" {
  name            = "acdl-microservice"
  cluster         = var.cluster_arn
  task_definition = aws_ecs_task_definition.this.arn
  desired_count   = var.desired_count
  launch_type     = var.launch_type

  network_configuration {
    subnets         = local.subnet_list
    security_groups = local.security_groups
    assign_public_ip = var.launch_type == "FARGATE"
  }

  dynamic "load_balancer" {
    for_each = var.lb_target_group_arn != null ? [1] : []
    content {
      target_group_arn = var.lb_target_group_arn
      container_name   = "app"
      container_port   = var.port
    }
  }
}
