ECS のスケジュールに基づくスケーリング設定で ObjectNotFoundException が発生するときの対処方法

2022.02.07

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

困っていた内容

ECS で「スケジュールに基づくスケーリング」を設定するため、put-scheduled-actionコマンドを実行したところ、ObjectNotFoundExceptionで失敗します。存在するリソース(resource-id)を指定しているのですが、なぜでしょうか。

$ aws application-autoscaling put-scheduled-action \
  --service-namespace ecs \
  --schedule "rate(1 hour)" \
  --timezone Asia/Tokyo \
  --scheduled-action-name hato-schedule-scale \
  --resource-id service/hato-cluster/hato-service \
  --scalable-dimension ecs:service:DesiredCount \
  --scalable-target-action MinCapacity=0,MaxCapacity=1

An error occurred (ObjectNotFoundException) when calling the PutScheduledAction operation: No scalable target registered for service namespace: ecs, resource ID: service/hato-cluster/hato-service, scalable dimension: ecs:service:DesiredCount

どう対応すればいいの?

スケーラブルターゲットへの登録有無をご確認ください。

$ aws application-autoscaling describe-scalable-targets --service-namespace ecs
{
    "ScalableTargets": []
}

「スケジュールされたスケーリング」で指定するリソース(resource-id)は、あらかじめスケーラブルターゲットとして登録する必要があります。

スケーラブルターゲットは、register-scalable-targetコマンドで登録でき、登録したスケーラブルターゲットはdescribe-scalable-targetsコマンドで確認できます。

「スケジュールされたスケーリング」で指定するリソースは、単純に存在するかではなく、スケーラブルターゲットとして登録しておく必要があることにご注意ください。

やってみた

register-scalable-targetコマンドを使用して、ECSサービスをスケーラブルターゲットに登録します。

 $ aws application-autoscaling register-scalable-target \
  --service-namespace ecs \
  --scalable-dimension ecs:service:DesiredCount \
  --resource-id service/hato-cluster/hato-service \
  --min-capacity 0 \
  --max-capacity 1
 $

describe-scalable-targetsコマンドで、スケーラブルターゲットに登録されたことを確認します。

$ aws application-autoscaling describe-scalable-targets --service-namespace ecs
{
    "ScalableTargets": [
        {
            "ServiceNamespace": "ecs",
            "ResourceId": "service/hato-cluster/hato-service",
            "ScalableDimension": "ecs:service:DesiredCount",
            "MinCapacity": 0,
            "MaxCapacity": 1,
            "RoleARN": "arn:aws:iam::123456789012:role/aws-service-role/ecs.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_ECSService",
            "CreationTime": "2021-12-21T00:00:00.000000+09:00",
            "SuspendedState": {
                "DynamicScalingInSuspended": false,
                "DynamicScalingOutSuspended": false,
                "ScheduledScalingSuspended": false
            }
        }
    ]
}

put-scheduled-actionコマンドで、スケーリングアクションを設定します。

$ aws application-autoscaling put-scheduled-action \
  --service-namespace ecs \
  --schedule "rate(1 hour)" \
  --timezone Asia/Tokyo \
  --scheduled-action-name hato-schedule-scale \
  --resource-id service/hato-cluster/hato-service \
  --scalable-dimension ecs:service:DesiredCount \
  --scalable-target-action MinCapacity=1,MaxCapacity=1
$

describe-scheduled-actionsコマンドで、スケーリングアクションが設定されたことを確認します。

$ aws application-autoscaling describe-scheduled-actions --service-namespace ecs
{
    "ScheduledActions": [
        {
            "ScheduledActionName": "hato-schedule-scale",
            "ScheduledActionARN": "arn:aws:autoscaling:ap-northeast-1:123456789012:scheduledAction:7da2b155-9d7f-4608-bce0-b4d3bfceeb6c:resource/ecs/service/hato-cluster/hato-service:scheduledActionName/hato-schedule-scale",
            "ServiceNamespace": "ecs",
            "Schedule": "rate(1 hour)",
            "Timezone": "Asia/Tokyo",
            "ResourceId": "service/hato-cluster/hato-service",
            "ScalableDimension": "ecs:service:DesiredCount",
            "ScalableTargetAction": {
                "MinCapacity": 0,
                "MaxCapacity": 1
            },
            "CreationTime": "2021-12-20T00:00:00.000000+09:00"
        }
    ]
}

参考資料