Search code examples
amazon-ecsaws-fargate

CPU/Memory setting for Task with more than 1 container


If I create a task running on fargate with more than 1 container. Ignore the fact that each container should be split out in to its on task. e.g. terraform code:

resource "aws_ecs_task_definition" "citi" {
  family                = "myfamily"
  execution_role_arn    = data.aws_iam_role.this.arn
  container_definitions = <<EOF
  [
    {
      "name": "myapp1",
      "image": "myapp1image",
      "portMappings": [
        {
          "containerPort": 10001
        }
      ]
    },
    {
      "name": "myapp2",
      "image": "myapp2image",
      "portMappings": [
        {
          "containerPort": 10004
        }
      ]
    }
  ]
  EOF
  cpu                      = 512
  memory                   = 1024
  requires_compatibilities = ["FARGATE"]
  network_mode             = "awsvpc"
}

Note: not specifying cpu and memory in containers.

  1. Should each container specify its cpu/memory value as best practise?
  2. If they do not as in the case above, how is cpu/memory allocated to each container? Do they get half overall e.g. Container 1 get cpu 256, memory 512 Or something else e.g. if Container 1 busy and Container 2 is not, can Container1 most of the tasks resources
  3. If each container has default value of essential set to true, does this mean if Container 1 stops, the task will stop and therefore Container 2 stops?

Solution

  • Short story: 1- it depends on the use case. If you want to optimize for simplicity you can avoid that and the two+ containers will fight for resources within the task. If you want to optimize for control you should configure that. 2- Yes (sort of). They are basically going to compete for all cpu/memory resources with even weights. 3- yes

    Long story: https://aws.amazon.com/blogs/containers/how-amazon-ecs-manages-cpu-and-memory-resources/