amazon-web-servicesterraformterraform-provider-aws

ECS Task definition CPU setting for terraform


I have this error, it shows the error CPU is not correct

Error: creating ECS Task Definition (wb_td): ClientException: Invalid 'cpu' setting for task.

with this source below.

resource "aws_ecs_task_definition" "this" {
  family                   = var.ecs_task_name
  network_mode             = "awsvpc"
  requires_compatibilities = ["FARGATE"]
  cpu                      = 1
  memory                   = 512
 #execution_role_arn       = aws_iam_role.ecs_execution.arn
 #task_role_arn            = aws_iam_role.ecs_task.arn
    
  container_definitions = jsonencode([{
    name      = "first"
    image     = "service-first"
    memory    = 128
    essential = true
    portMappings = [
      {
         containerPort = 80
         hostPort      = 80
      }
    ]
    },
    {
       name      = "second"
       image     = "service-second"
       memory    = 128
       essential = true
       portMappings = [
         {
           containerPort = 443
           hostPort      = 443
         }
       ]
     }
  ])
    
  tags = {
    Name = var.ecs_task_name
  }
}

Solution

  • I think you are using a wrong CPU/memory combination. The full list is defined in the AWS docs. Based on this terraform code:

      cpu    = 1
      memory = 512
    

    It should be switched to either cpu = "256" (if you want to keep 512MB of memory):

      cpu    = "256"
      memory = "512"
    

    If you still want to have 1vCPU, then you need more memory, starting with 2GB minimum. In that case that should be defined as:

      cpu    = "1024"
      memory = "2048"
    

    Fargate Tasks