How to scale ECS sidecar containers independently

702 Views Asked by At

How can I create and independently scale sidecar containers in ECS Fargate using the AWS console?

The task creation step allows adding multiple containers with different CPU and memory configurations but not an independent scaling option. On the other hand, the ECS Service launch allows the option to scale only at the task level. Also, ECS doesn't clearly mention how a container can be specified as a sidecar.

1

There are 1 best solutions below

0
Fermin On

You can't independently scale a sidecar in ECS. The unit of scaling in ECS is at the task level.

You can specify cpu and memory of the Fargate task (e.g. 512/1024) - this is the resources that are assigned for that task and are what you will pay for on your bill.

Within that task you can have 1:n containers - each of these can have their own cpu and memory configurations but these are assigned within the constraints of the task - the combined cpu/memory values for all containers cannot exceed those assigned to the task (e.g. you couldn't have a 512/1024 task and assign 2048 memory to a container within it).

This effectively allows you to give weighting to the containers in your task, e.g. giving an nginx sidecar less of a weighting than your main application.

ECS doesn't clearly mention how a container can be specified as a sidecar.

A 'sidecar' is just a container that shares resources (network, disk etc) with another container. Creating a task definition with 2 containers gives you a sidecar.

Below is a sample task-definition that has nginx fronting a Flask app. It contains:

  • A flask app listening on port 5000.
  • An nginx container listening on port 80. This has an envvar upstream=http:\\localhost:5000. As these both share the same network they can communicate via localhost (so nginx could forward to flask-app).
  • They both have access to a shared drive ("shared-volume")
{
    "containerDefinitions": [
        {
            "name": "main-app",
            "image": "ghcr.io/my-flask-app",
            "portMappings": [
                {
                    "containerPort": 5000,
                    "hostPort": 5000,
                    "protocol": "tcp"
                }
            ],
            "mountPoints": [
                {
                    "sourceVolume": "shared-volume",
                    "containerPath": "/scratch"
                }
            ]
        },
        {
            "name": "sidecar",
            "image": "ghcr.io/my-nginx",
            "portMappings": [
                {
                    "containerPort": 80,
                    "hostPort": 80,
                    "protocol": "tcp"
                }
            ],
            "environment": [
                {
                    "name": "upstream",
                    "value": "http:\\localhost:5000"
                }
            ],
            "mountPoints": [
                {
                    "sourceVolume": "shared-volume",
                    "containerPath": "/scratch"
                }
            ]
        }
    ],
    "networkMode": "awsvpc",
    "revision": 1,
    "volumes": [
        {
            "name": "shared-volume",
            "host": {}
        }
    ],
    "requiresCompatibilities": [
        "FARGATE"
    ],
    "cpu": "512",
    "memory": "1024"
}