

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 因應 Amazon ECS 限流問題
<a name="operating-at-scale-dealing-with-throttles"></a>

限流錯誤分為兩個主要類別：同步限流與非同步限流。

## 同步限流
<a name="synchronous-throttling"></a>

發生同步限流時，您會立即收到來自 Amazon ECS 的錯誤回應。如果您在執行任務或建立服務時呼叫 Amazon ECS API，通常會發生此類別錯誤。如需有關所涉限流問題與相關限流限制的詳細資訊，請參閱 [Request throttling for the Amazon ECS API](https://docs.aws.amazon.com/AmazonECS/latest/APIReference/request-throttling.html)。

當您的應用程式啟動 API 請求時，例如使用 AWS CLI 或 AWS SDK，您可以修復 API 限流。具體可透過兩種方式實現：一是將應用程式架構設計為可處理這些錯誤，二是為 API 呼叫視作具有重試邏輯的指數退避與抖動策略。如需詳細資訊，請參閱 [Timeouts, retries, and backoff with jitter](https://aws.amazon.com/builders-library/timeouts-retries-and-backoff-with-jitter/)。

如果您使用 AWS SDK，則自動重試邏輯是內建且可設定的。

## 非同步限流
<a name="asynchronous-throttling"></a>

非同步限流的發生源於非同步工作流程，在該流程中，Amazon ECS 或 CloudFormation 可能會代表您呼叫 API 來佈建資源。請務必了解 Amazon ECS 代您叫用哪些 AWS APIs。例如，對於使用 `awsvpc` 網路模式的任務，系統會調用 `CreateNetworkInterface` API；而對已註冊至負載平衡器的任務執行運作狀態檢查時，則會調用 `DescribeTargetHealth` API。

當工作負載達到相當大的規模時，這些 API 操作可能會受到限流。也就是說，它們可能會受到足夠限流，違反 Amazon ECS 或正在呼叫 AWS 服務 的 強制執行的限制。例如，若同時部署數百個服務，每個服務都有數百個使用 `awsvpc` 網路模式的任務，Amazon ECS 會分別呼叫 Amazon EC2 的 API 操作 (如 `CreateNetworkInterface`) 以及 Elastic Load Balancing 的 API 操作 (如 `RegisterTarget` 或 `DescribeTargetHealth`)，完成彈性網路介面與負載平衡器的註冊工作。這些 API 呼叫可能會超過 API 限制，導致限流錯誤。以下是服務事件訊息中包含的 Elastic Load Balancing 限流錯誤範例。

```
{
   "userIdentity":{
      "arn":"arn:aws:sts::111122223333:assumed-role/AWSServiceRoleForECS/ecs-service-scheduler",
      "eventTime":"2022-03-21T08:11:24Z",
      "eventSource":"elasticloadbalancing.amazonaws.com",
      "eventName":" DescribeTargetHealth ",
      "awsRegion":"us-east-1",
      "sourceIPAddress":"ecs.amazonaws.com",
      "userAgent":"ecs.amazonaws.com",
      "errorCode":"ThrottlingException",
      "errorMessage":"Rate exceeded",
      "eventID":"0aeb38fc-229b-4912-8b0d-2e8315193e9c"
   }
}
```

當這些 API 呼叫與您帳戶中的其他 API 流量共用限制時，即使其作為服務事件發出，仍可能難以監控。

## 監控限流
<a name="monitoring-throttling"></a>

請務必確定受到限流的具體 API 請求以及請求發起來源。您可以使用 AWS CloudTrail 來監控限流，並與 CloudWatch、Amazon Athena 和 Amazon EventBridge 整合。您可以設定 CloudTrail 將特定事件傳送至 CloudWatch Logs。CloudWatch Logs 日誌洞察會剖析並分析事件。這可確定限流事件中的詳細資訊，例如進行呼叫的使用者或 IAM 角色，以及進行的 API 呼叫數量。如需詳細資訊，請參閱 [Monitoring CloudTrail log files with CloudWatch Logs](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/monitor-cloudtrail-log-files-with-cloudwatch-logs.html)。

如需有關使用 CloudWatch Logs Insights 的詳細資訊以及有關如何查詢日誌檔案的說明，請參閱 [Analyzing log data with CloudWatch Logs Insights](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/AnalyzingLogData.html)。

透過 Amazon Athena，您可以使用標準 SQL 建立查詢並分析資料。例如，您可以建立 Athena 表格來剖析 CloudTrail 事件。如需詳細資訊，請參閱 [Using the CloudTrail console to create an Athena table for CloudTrail logs](https://docs.aws.amazon.com/athena/latest/ug/cloudtrail-logs.html#create-cloudtrail-table-ct)。

建立 Athena 表格之後，您可以使用 SQL 查詢 (例如下列查詢) 來調查 `ThrottlingException` 錯誤。

將 {{user-input}} 取代為實際值。

```
select eventname, errorcode,eventsource,awsregion, useragent,COUNT(*) count
FROM cloudtrail_{{table-name}}
where errorcode = 'ThrottlingException'
AND eventtime between '{{2024-09-24T00:00:08Z}}' and '{{2024-09-23T23:15:08Z}}'
group by errorcode, awsregion, eventsource, useragent, eventname
order by count desc;
```

Amazon ECS 也會將事件通知發送至 Amazon EventBridge。事件主要分為資源狀態變更事件與服務操作事件兩大類。其中包含 API 限流事件，例如 `ECS_OPERATION_THROTTLED` 與 `SERVICE_DISCOVERY_OPERATION_THROTTLED`。如需詳細資訊，請參閱[Amazon ECS 服務動作事件](ecs_service_events.md)。

這些事件可由 等服務取用 AWS Lambda ，以執行動作來回應。如需詳細資訊，請參閱[因應 Amazon ECS 事件](ecs_cwet_handling.md)。

如果執行獨立任務，某些 API 操作 (例如 `RunTask`) 是非同步的，且不會自動執行重試操作。在這種情況下，您可以使用 等服務 AWS Step Functions 搭配 EventBridge 整合，重試調節或失敗的操作。如需詳細資訊，請參閱 [Manage a container task (Amazon ECS, Amazon SNS)](https://docs.aws.amazon.com/step-functions/latest/dg/sample-project-container-task-notification.html)。

## 使用 CloudWatch 監控限流
<a name="monitoring-throttling-cw"></a>

CloudWatch 會在**依 AWS 資源**下的`Usage`命名空間上提供 API 用量監控。這些指標會以類型 **API** 與指標名稱 **CallCount** 記錄。您可以建立警示，在這些指標達到特定閾值時啟動。如需詳細資訊，請參閱 [Visualizing your service quotas and setting alarms](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-Quotas-Visualize-Alarms.html)。

CloudWatch 也提供異常偵測功能。此功能使用機器學習，根據您所啟用指標的特定行為進行分析並建立基準。如有異常的 API 活動，您可以將此功能與 CloudWatch 警示搭配使用。如需詳細資訊，請參閱 [Using CloudWatch anomaly detection](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Anomaly_Detection.html)。

透過主動監控限流錯誤，您可以聯絡 支援 來提高相關的限流限制，並收到您獨特應用程式需求的指引。