2022-12-04

The Container Does not Exist in the Task Definition

The Container Does not Exist in the Task Definition

When applying the following Terraform code to create an ECS service, we will encounter an error message saying 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" {

Solution

The container name defined in aws_ecs_task_definition needs to match the container_name configuration in aws_ecs_service's load_balancer settings. By modifying aws_ecs_service as shown below, you will be able to successfully apply the changes.

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

References

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!