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달 전197회 조회
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.

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠