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
已提问 3 个月前196 查看次数
1 回答
2
已接受的回答

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
专家
已回答 3 个月前
profile picture
专家
已审核 2 个月前
profile picture
专家
Kallu
已审核 2 个月前
  • 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.

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则