ECS PlacementStrategy setup with Terraform but not working as intended

0

Hello, Fellows!

This is my first question on re:Post. I'm glad to see you all! I'm investigating an issue where the ECS Placement strategy is not working as intended.

We've set up the ECS tasks with 2 services in the cluster.

Below is the source code for the module we've configured:

  dynamic "ordered_placement_strategy" {
    for_each = var.scheduling_strategy == "REPLICA" ? [1] : []
    content {
      type  = "spread"
      field = "attribute:ecs.availability-zone"
    }
  }

  dynamic "ordered_placement_strategy" {
    for_each = var.scheduling_strategy == "REPLICA" ? [1] : []
    content {
      type  = "spread"
      field = "instanceId"
    }
  }

Even though it seems to have been set up correctly, and it's all deployed as REPLICA, it's not working as intended.

  "PlacementStrategy": [
    {
      "type": "spread",
      "field": "attribute:ecs.availability-zone"
    },
    {
      "type": "spread",
      "field": "instanceId"
    }
  ],

After the cluster was set up, it appears that the tasks from service-b are placed on one instance (e.g., in AZ-B).

If you have any ideas, please let me know.

  • How many tasks in service b? How many configured containers for service b?

  • Thanks for the reply. There are only 2 tasks in service b. And only one container for one service each.

  • Thanks Cy.. I see the issue..

profile picture
Cy_Choi
asked 2 months ago175 views
1 Answer
2
Accepted Answer

Hey Cy, the issue is not with the Task Placement but with the Task Constraint. You want the ECS to spread the tasks on different instances so that if an EC2 dies you have spread the tasks to other EC2s etc.. You need to setup a Constraint https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-placement-constraints.html

You will need to setup distinctInstance

  • Place each task on a different container instance. This task placement constraint can be specified when either running a task or creating a new service.
"placementConstraints": [
    {
        "type": "distinctInstance"
    }
]

In the aws_ecs_service resource in TF, you will need

placement_constraints {
    type       = "distinctInstance"
  }

placement_constraints- (Optional) Rules that are taken into consideration during task placement. Updates to this configuration will take effect next task deployment unless force_new_deployment is enabled

Task placement strategies are a best effort. Amazon ECS still attempts to place tasks even when the most optimal placement option is unavailable. However, task placement constraints are binding, and they can prevent task placement.

profile picture
EXPERT
answered 2 months ago
profile picture
EXPERT
reviewed a month ago
profile picture
EXPERT
Kallu
reviewed 2 months ago
  • Thanks, Gary! It worked for me.

    Is there a base version that corresponds to the behavior of this content? I only included ordered_placement_strategy in a new test cluster but it behaved the same as when distinctInstance was not included.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions