Traffine I/O

Bahasa Indonesia

2022-12-04

The Container Does not Exist in the Task Definition

Kontainer Tidak Ada dalam Definisi Task

Ketika menerapkan kode Terraform berikut untuk membuat layanan ECS, kita akan menemui pesan kesalahan yang mengatakan The container does not exist in the task definition.

resource "aws_ecs_task_definition" "fastapi" {
  family                   = "${var.app_name}-fastapi"
  requires_compatibilities = ["FARGATE"]
  network_mode             = "awsvpc"
  execution_role_arn       = aws_iam_role.ecs_execution_role.arn
  task_role_arn            = aws_iam_role.ecs_execution_role.arn
  cpu                      = 256
  memory                   = 512
  container_definitions = jsonencode([
    {
      name    = "${var.app_name}-fastapi"
      image   = "${data.aws_ecr_repository.fastapi.repository_url}:latest"
      command = ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80"]
      portMappings = [
        {
          hostPort      = 80
          containerPort = 80
          protocol      = "tcp"
        }
      ],
      logConfiguration = {
        logDriver = "awslogs"
        options = {
          awslogs-region        = "ap-northeast-1"
          awslogs-stream-prefix = "ecs"
          awslogs-group         = aws_cloudwatch_log_group.ecs.name
        }
      },
      environment = []
    }
  ])
}

resource "aws_ecs_service" "fastapi" {
  name            = "${var.app_name}-fastapi"
  cluster         = aws_ecs_cluster.main.name
  launch_type     = "FARGATE"
  desired_count   = 1
  task_definition = aws_ecs_task_definition.fastapi.arn

  network_configuration {
    subnets         = [for s in aws_subnet.privates : s.id]
    security_groups = [aws_security_group.ecs.id]
  }
  load_balancer {
    target_group_arn = aws_lb_target_group.main.arn
    container_name   = "my-fastapi"
    container_port   = "80"
  }

  lifecycle {
    ignore_changes = [
      desired_count,
    ]
  }

  depends_on = [aws_lb_listener_rule.main]
}
│ Error: creating ECS Service (fastapi): InvalidParameterException: The container my-fastapi does not exist in the task definition.
│
│   with aws_ecs_service.fastapi,
│   on ecs.tf line 40, in resource "aws_ecs_service" "fastapi":40: resource "aws_ecs_service" "fastapi" {

Solusi

Nama kontainer yang didefinisikan dalam aws_ecs_task_definition harus sesuai dengan konfigurasi container_name dalam pengaturan load_balancer dari aws_ecs_service. Dengan memodifikasi aws_ecs_service seperti yang ditunjukkan di bawah ini, Anda akan dapat berhasil apply perubahan tersebut.

 resource "aws_ecs_service" "fastapi" {
   name            = "${var.app_name}-fastapi"
   cluster         = aws_ecs_cluster.main.name
   launch_type     = "FARGATE"
   desired_count   = 1
   task_definition = aws_ecs_task_definition.fastapi.arn

   network_configuration {
     subnets         = [for s in aws_subnet.privates : s.id]
     security_groups = [aws_security_group.ecs.id]
   }
   load_balancer {
     target_group_arn = aws_lb_target_group.main.arn
-     container_name   = "my-fastapi"
+     container_name   = "${var.app_name}-fastapi"
     container_port   = "80"
   }

   lifecycle {
     ignore_changes = [
       desired_count,
     ]
   }

   depends_on = [aws_lb_listener_rule.main]
 }

Referensi

https://stackoverflow.com/questions/62104862/the-container-does-not-exist-in-the-task-definition

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!