ECS Task Placement

When the task of EC2 is launched. ECS must figure out where to place it given CPU, Memory and available port. It also needs to know which task to terminate when ECS scales in

To do this, you need to define task placement strategy and task placement constraints

Note: this is only for ECS EC2

Task placement strategies

  • BinPack
    • Most cost saving
    • Try to fill in one EC2 as much as possible before creating a new one
"placementStrategy": [
	{
		"field": "memory", // or cpu
		"type": "binpack"
	}
]
  • Random
    • Place the task randomly across the EC2 instance
"placementStrategy": [
	{
		"type": "random"
	}
]
  • Spread
    • spread across specific values. For example, instance id or availability zone
"placementStrategy": [
	{
		"type": "spread",
		"field": "attribute:ecs.availability-zone"
	}
]

You can also mix them together, for example

"placementStrategy": [
	{
		"type": "spread",
		"field": "attribute:ecs.availability-zone"
	},
		{
		"field": "memory", // or cpu
		"type": "binpack"
	}
]

Task placement constraints

  • distinctInstance: place each task on a different container instance
"placementConstraints": [
	{
		"type": "distinctInstance"
	}
]
  • memberOf: place task on instances that satisfy an expression
    • Uses Cluster Query Language
"placementConstraints": [
	{
		"expression": "attribute:ecs.instance-type =~ t2.*",
		"type": "memberOf"
	}
]