

# REL06-BP03 傳送通知 (即時處理和警示)
<a name="rel_monitor_aws_resources_notification_monitor"></a>

當組織偵測到潛在問題時，他們會將即時通知和警示傳送給適當的人員和系統，以便快速有效地應對這些問題。

 **預期成果：** 根據服務和應用程式指標設定相關警示，就可以快速回應操作事件。違反警示閾值時，系統會通知適當的人員和系統，以便解決潛在問題。 

 **常見的反模式：** 
+ 將警示的閾值設得過高，會導致無法傳送重要通知。
+ 將警示的閾值設得太低，導致使用者因通知過多的干擾而無法針對重要提醒採取行動。
+  當使用情況改變時，未更新警示及其閾值。 
+  針對透過自動化動作解決的最佳警示，將通知傳送給人員而未引發自動化動作，會導致傳送過多的通知。 

 **建立此最佳實務的優勢：** 將即時通知和警示傳送給適當的人員和系統，以便及早發現問題並快速回應操作事故。 

 **未建立此最佳實務時的曝險等級：** 高 

## 實作指引
<a name="implementation-guidance"></a>

 工作負載應具備即時處理和警示功能，以改善可能影響應用程式可用性問題的可偵測性，並作為自動化回應的觸發程式。組織可以透過使用已定義的指標建立警示來執行即時處理和警示，以便在發生重大事件或指標超過閾值時收到通知。 

 [Amazon CloudWatch](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/WhatIsCloudWatch.html) 可讓您建立 [指標](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/working_with_metrics.html) 和複合警示，過程中根據靜態閾值、異常偵測和其他條件使用 CloudWatch 警示。如需有關可使用 CloudWatch 設定的警示類型詳細資訊，請參閱 [CloudWatch 文件的警示一節](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/AlarmThatSendsEmail.html)。 

 您可以為團隊建構 AWS 資源的指標和警示自訂檢視，過程中使用 [CloudWatch 儀表板](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Dashboards.html)。您可以透過 CloudWatch 主控台中的可自訂首頁，在單一檢視中監控多個區域的資源。 

 警示可以執行一或多個動作，例如將通知傳送給 [或向 Amazon SNS 主題](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/US_SetupSNS.html)、執行 [Amazon EC2](https://aws.amazon.com/ec2/) 動作或 [Amazon EC2 Auto Scaling](https://aws.amazon.com/ec2/autoscaling/) 動作，或 [建立 OpsItem](https://docs.aws.amazon.com/systems-manager/latest/userguide/OpsCenter-create-OpsItems-from-CloudWatch-Alarms.html) 或 [事故](https://docs.aws.amazon.com/incident-manager/latest/userguide/incident-creation.html) (AWS Systems Manager)。 

 Amazon CloudWatch 使用 [Amazon SNS](https://docs.aws.amazon.com/sns/latest/dg/welcome.html) ，於警示變更狀態時傳送通知，將訊息傳遞從發布者 (生產者) 提供給訂閱用戶 (消費者)。如需設定 Amazon SNS 通知的詳細資訊，請參閱 [設定 Amazon SNS](https://docs.aws.amazon.com/sns/latest/dg/sns-configuring.html)。 

 CloudWatch 傳送 [EventBridge](https://aws.amazon.com/eventrbridge/) [的安全](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch-and-eventbridge.html) 事件，尤其是每當在 CloudWatch 警示進行建立、更新、刪除，或是狀態變更時。您可以使用 EventBridge 搭配這些事件來建立執行動作的規則，例如，當警示狀態變更時就通知您，或自動觸發在您帳戶中的事件，過程是使用 [Systems Manager 自動化功能](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-automation.html)。 

** 何時應該使用 EventBridge 和 Amazon SNS？ **

 EventBridge 和 Amazon SNS 可用於開發事件驅動的應用程式，將視具體需求來做出選擇。 

 在建置應用程式以回應您自己應用程式、SaaS 應用程式和 AWS 服務中的事件時，建議使用 Amazon EventBridge。EventBridge 是唯一直接與第三方 SaaS 合作夥伴整合的事件型服務。EventBridge 還會自動從 200 多個 AWS 服務中擷取事件，而不需開發人員在帳戶中建立任何資源。 

 EventBridge 會將已定義的 JSON 架構用在事件，並協助您建立在整個事件內文套用的規則，以選取要轉寄至 [目標的事件](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-targets.html)。EventBridge目前支援 20 多項 AWS 服務做為目標，包括 [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html)、[Amazon SQS](https://aws.amazon.com/sqs/)、Amazon SNS、[Amazon Kinesis Data Streams](https://aws.amazon.com/kinesis/data-streams/)和 [Amazon Data Firehose](https://aws.amazon.com/kinesis/data-firehose/)。 

 針對需要高散發的應用程式 (數千或數百萬個端點)，建議使用 Amazon SNS。我們看到的常見模式是客戶使用 Amazon SNS 做為規則的目標，以篩選所需的事件並散發到多個端點。 

 訊息是非結構化的，且可以是任何格式。Amazon SNS 支援將訊息轉寄至六種不同的目標，包括 Lambda、Amazon SQS、HTTP/S 端點、SMS、行動推送和電子郵件。Amazon SNS [一般的延遲時間短於 30 毫秒](https://aws.amazon.com/sns/faqs/)。透過將服務設定為傳送 AWS 訊息，各種 Amazon SNS 服務就能做到這一點 (超過 30 個，包括 Amazon EC2、[Amazon S3](https://aws.amazon.com/s3/)和 [Amazon RDS](https://aws.amazon.com/rds/))。 

### 實作步驟
<a name="implementation-steps"></a>

1.  建立警示，過程中使用 [Amazon CloudWatch 警示](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/AlarmThatSendsEmail.html)。 

   1.  指標警示會監控單一 CloudWatch 指標或與 CloudWatch 指標相依的表達式。與超過一段時間間隔的閾值相比，警示會根據指標或表達式的值起始一或多個動作。此動作可能包括將通知傳送給 [或向 Amazon SNS 主題](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/US_SetupSNS.html)、執行 [Amazon EC2](https://aws.amazon.com/ec2/) 動作或 [Amazon EC2 Auto Scaling](https://aws.amazon.com/ec2/autoscaling/) 動作，或 [建立 OpsItem](https://docs.aws.amazon.com/systems-manager/latest/userguide/OpsCenter-create-OpsItems-from-CloudWatch-Alarms.html) 或 [事故](https://docs.aws.amazon.com/incident-manager/latest/userguide/incident-creation.html) (AWS Systems Manager)。 

   1.  複合警示由規則表達式組成，該規則表達式會將您已建立的其他警示條件納入考量。只有在符合所有規則條件時，複合警示才會進入警示狀態。在複合警示規則表達式中指定的警示可能會包括指標警示和其他複合警示。複合警示可在狀態變更時傳送 Amazon SNS 通知，並可建立 Systems Manager [建立和追蹤這些改善](https://docs.aws.amazon.com/systems-manager/latest/userguide/OpsCenter-create-OpsItems-from-CloudWatch-Alarms.html) 或 [事故](https://docs.aws.amazon.com/incident-manager/latest/userguide/incident-creation.html) (在進入警示狀態時)，但其無法執行 Amazon EC2 或 Auto Scaling 動作。 

1.  設定 [Amazon SNS 通知](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/US_SetupSNS.html)。您可以在建立 CloudWatch 警示時，包含在警示變更狀態時傳送通知的 Amazon SNS 主題。 

1.  [在 EventBridge 中建立規則](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-get-started.html) 且此規則會比對指定的 CloudWatch 警示。每個規則都支援包括 Lambda 函數的多個目標。例如，您可以定義在可用磁碟空間不足時啟動的警示，該警示會透過 EventBridge 規則觸發 Lambda 函數以清理空間。如需 EventBridge 目標的詳細資訊，請參閱 [EventBridge 目標](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-targets.html)。 

## 資源
<a name="resources"></a>

 **相關 Well-Architected 的最佳實務：** 
+  [REL06-BP01 監控工作負載的所有元件 (產生)](rel_monitor_aws_resources_monitor_resources.md) 
+  [REL06-BP02 定義和計算指標 (彙總)](rel_monitor_aws_resources_notification_aggregation.md) 
+  [REL12-BP01 使用程序手冊調查失敗](rel_testing_resiliency_playbook_resiliency.md) 

 **相關文件：** 
+ [ Amazon CloudWatch ](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/WhatIsCloudWatch.html)
+ [ CloudWatch Logs 洞見 ](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/AnalyzingLogData.html)
+  [使用 Amazon CloudWatch 警示](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/AlarmThatSendsEmail.html) 
+  [使用 Amazon CloudWatch 儀表板](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Dashboards.html) 
+  [使用 Amazon CloudWatch 指標](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/working_with_metrics.html) 
+ [ 設定 Amazon SNS 通知 ](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/US_SetupSNS.html)
+ [ CloudWatch 異常偵測 ](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Anomaly_Detection.html)
+ [ CloudWatch Logs 資料保護 ](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/protect-sensitive-log-data-types.html)
+ [ Amazon EventBridge ](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-what-is.html)
+ [ Amazon Simple Notification Service ](https://aws.amazon.com/sns/)

 **相關影片：** 
+ [ reinvent 2022 可觀測性影片 ](https://www.youtube.com/results?search_query=reinvent+2022+observability)
+ [AWS re:Invent 2022 - Amazon 的可觀測性最佳實務 ](https://www.youtube.com/watch?v=zZPzXEBW4P8)

 **相關範例：** 
+  [One Observability 研討會](https://observability.workshop.aws/) 
+ [ Amazon EventBridge 到 AWS Lambda，由 Amazon CloudWatch 警示進行回饋控制 ](https://serverlessland.com/patterns/cdk-closed-loop-serverless-control-pattern)