Traffine I/O

日本語

2022-12-04

The Container Does not Exist in the Task Definitionエラー

The Container Does not Exist in the Task Definition

ECS Serviceを作成する以下のTerraformのコードをapplyすると、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" {

解決法

aws_ecs_task_definitionで定義するコンテナ名は、aws_ecs_serviceload_balancer設定のcontainer_nameのものと一致させる必要があります。以下のようにaws_ecs_serviceを修正するとapplyできるようになります。

 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]
 }

参考

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!