AWS-ECS-Task 完成任务后如何停止?

luc*_*cas 6 amazon-web-services amazon-ecs terraform

我部署了一个 ECS 任务,它可以备份数据库。完成此操作后,任务仍处于运行状态。有人知道如何结束任务吗?

我使用 Docker 容器(Spring Boot)并使用 Terraform 部署任务。

编辑:我的 Terraform 看起来像这样:

resource "aws_ecs_task_definition" "task_definition" {
  family                   = "${var.application_name}"
  container_definitions    =  "${data.template_file.container_definition_tpl.rendered}"
  requires_compatibilities = ["FARGATE"]
  network_mode             = "awsvpc"
  execution_role_arn       = "arn:aws:iam::${var.account_id}:role/app_execution_role"
  task_role_arn            = "arn:aws:iam::${var.account_id}:role/app_task_role"
  cpu                      = 256
  memory                   = 512
}

resource "aws_cloudwatch_event_rule" "scheduled_task" {
  name                = "${var.name}_${var.environment}_scheduled_task"
  description         = "Run ${var.name}_${var.environment} task at a scheduled time rate (1 day)"
  schedule_expression = "cron(* 9 * ? *)"
}

resource "aws_cloudwatch_event_target" "scheduled_task" {
  target_id = "${var.name}_${var.environment}_scheduled_task_target"
  rule      = "${aws_cloudwatch_event_rule.scheduled_task.name}"
  arn       = "${data.aws_ecs_cluster.ecs_cluster.id}"
  role_arn  = "${aws_iam_role.ecs_events.arn}"

  ecs_target {
    task_count          =  1
    task_definition_arn = "${aws_ecs_task_definition.task_definition.arn}"
    launch_type="FARGATE"
    platform_version="LATEST"


    network_configuration {
      assign_public_ip = false
      security_groups= [data.aws_security_group.ecs_security_group.id]
      subnets =data.aws_subnet_ids.private_subnet_ids.ids
    }
  } 
Run Code Online (Sandbox Code Playgroud)

我有一个每天运行一次的计划任务。但它创建了一个新任务,而另一个任务仍在运行,所以一周后我将有 7 个任务。

luc*_*cas 4

最后解决方案非常简单。

问题:
该任务在 ECS-Serivce 中运行,也称为计划事件。这导致了很多问题,因为任务在给定时间运行了两次。

解决方案
最后,解决方案是删除ECS服务,因为该作业不满足ECS服务的要求。答案是仅通过 Schedules 任务调用该任务。另外,实现时必须关闭容器,这确保了当任务完成时,任务会自动关闭,不会有不必要的任务。

  • “实施必须关闭容器” - 你是怎么做到的? (4认同)
  • 为了停止任务,容器需要退出,为此,在容器中运行的程序/命令只需要以状态代码 0 存在。 (2认同)