Amazon ECSのサービスを複数のロードバランサに追加する

サービスを複数のロードバランサに追加したい

Amzon ECSでサービスを作ると、そのサービスから立ち上がったコンテナを、ALB(Application Load Balancer)のターゲットとして追加することができます。

しかし、追加できるALBは1つまでです。

たとえば内部ALBとインターネット向けALBを分けている場合など、複数のロードバランサがある場合、サービスを複数作る必要があります。

実は複数ロードバランサをサポートしていた

調べてみると、以下のようなリリースが2019年にされていました。

Amazon ECS サービスで複数のロードバランサーターゲットグループのサポートを開始

しかし、マネジメントコンソールではこの設定のUIが提供されていないようで、AWS CLIなどを利用してサービスを作成する必要があります。

AWS CLIからサービスを作成する

ロードバランサを指定できるのは、作成時のみです。

そのため、サービスの作成時点で複数のロードバランサを指定します。

aws ecs create-service \
--cluster cluster-xxxx \
--service-name service-xxxx \
--task-definition arn:aws:ecs:ap-northeast-1:xxxxxxxxxxxxxx:task-definition/xxxx:x \
--load-balancers '[{"targetGroupArn": "arn:aws:elasticloadbalancing:ap-northeast-1:xxxxxxxxxxxxxx:targetgroup/xxxx/xxxxxxxx","containerName": "xxxx","containerPort": 80},{"targetGroupArn": "arn:aws:elasticloadbalancing:ap-northeast-1:xxxxxxxxxxxxxx:targetgroup/xxxx/xxxxxxxx","containerName": "xxxx","containerPort": 80}]' \
--desired-count 2 \
--launch-type EC2 \
--deployment-configuration '{"deploymentCircuitBreaker": {"enable": false,"rollback": false},"maximumPercent": 100,"minimumHealthyPercent": 50}' \
--placement-strategy '[{"type": "spread","field": "attribute:ecs.availability-zone"},{"type": "spread","field": "instanceId"}]' \
--health-check-grace-period-seconds 0 \
--scheduling-strategy REPLICA \
--enable-ecs-managed-tags

重要なのは、 –load-balancers のオプションです。

[
  {
    "targetGroupArn": "arn:aws:elasticloadbalancing:ap-northeast-1:xxxxxxxxxxxxxx:targetgroup/xxxx/xxxxxxxx",
    "containerName": "xxxx",
    "containerPort": 80
  },
  {
    "targetGroupArn": "arn:aws:elasticloadbalancing:ap-northeast-1:xxxxxxxxxxxxxx:targetgroup/xxxx/xxxxxxxx",
    "containerName": "xxxx",
    "containerPort": 80
  }
]

これで、複数ロードバランサで利用できるサービスを作成できました。

タイトルとURLをコピーしました