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
エキスパート
回答済み 2ヶ月前
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.

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ