Swap space use for AWS ECS Cluster instances

Solution 1:

With ECS you have to specify the Memory Reservation for each task and ECS will not let you oversubscribe the available memory. I.e. if your instance has 4GB RAM and you take away a bit for the OS (say 0.5GB) you are left with 3.5GB for your tasks. You can create a 1.5GB task and 2GB task but can't have 2x 2GB tasks.

What that means is that swap space is hardly ever used on ECS nodes - the tasks should fit in the available memory.

Also it's questionable whether r5 is more expensive than m5d - it depends if you are constrained by CPU or RAM. As of today r5.large with 16GB RAM costs $0.126/hr while a comparable m5d.xlarge with 16GB RAM costs $0.226/hr - quite a bit more. With M5D you are getting both RAM, CPU and Disk, hence it's more expensive than RAM-optimised R5 class.

If I were you I would look at AWS Saving Plans that give you much higher flexibility over Reserved Instances. You can choose instance classes, size, and even run your containers in Fargate.

Hope that helps :)

Solution 2:

Swap space is useful or at least usable for ECS cluster instances. You can find a pretty good documentation in the Amazon ECS Developer Guide.

Basically there are two task definition parameters you can use to control the swap usage: maxSwap and swappiness. Even if you don't provide any of these parameters by default ECS will make use of the swap space. In order to avoid swap usage you need to set the values to 0.