

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

# 在 Amazon SNS 中記錄和監控
<a name="sns-logging-monitoring"></a>

Amazon SNS 可讓您使用 CloudTrail 記錄 API 呼叫，並使用 CloudWatch 監控主題，以追蹤和監控訊息活動。這些工具可協助您深入了解訊息傳遞、疑難排解問題，並確保訊息工作流程的運作狀態。本主題涵蓋下列項目：
+ [使用 記錄 AWS SNS API 呼叫 AWS CloudTrail](logging-using-cloudtrail.md)。 此記錄可讓您追蹤在 Amazon SNS 主題上執行的動作，例如主題建立、訂閱管理和訊息發佈。透過分析 CloudTrail 日誌，您可以識別提出特定 API 請求的人員，以及提出這些請求的時間，協助您稽核和疑難排解 Amazon SNS 用量。
+ [使用 Amazon CloudWatch 監控 Amazon SNS 主題](sns-monitoring-using-cloudwatch.md)。 CloudWatch 提供指標，可讓您即時觀察 Amazon SNS 主題的效能和運作狀態。根據這些指標設定警示，讓您能夠快速回應任何異常，例如交付失敗或高訊息延遲。此監控功能可確保您可以透過主動解決潛在問題來維護 SNS 型傳訊系統的可靠性。

# 使用 記錄 AWS SNS API 呼叫 AWS CloudTrail
<a name="logging-using-cloudtrail"></a>

AWS SNS 已與 整合[AWS CloudTrail](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-user-guide.html)，此服務提供使用者、角色或 所採取動作的記錄 AWS 服務。CloudTrail 會將 SNS 的所有 API 呼叫擷取為事件。擷取的呼叫包括來自 SNS 主控台的呼叫，以及對 SNS API 操作的程式碼呼叫。使用 CloudTrail 收集的資訊，您可以判斷向 SNS 提出的請求、提出請求的 IP 地址、提出請求的時間，以及其他詳細資訊。

每一筆事件或日誌專案都會包含產生請求者的資訊。身分資訊可協助您判斷下列事項：
+ 該請求是使用根使用者還是使用者憑證提出。
+ 請求是否代表 IAM Identity Center 使用者提出。
+ 提出該請求時，是否使用了特定角色或聯合身分使用者的暫時安全憑證。
+ 該請求是否由另一項 AWS 服務服務提出。

當您建立帳戶 AWS 帳戶 時CloudTrail 會在 中處於作用中狀態，而且您會自動存取 CloudTrail **事件歷史記錄**。CloudTrail **事件歷史記錄**為 AWS 區域中過去 90 天記錄的管理事件，提供可檢視、可搜尋、可下載且不可變的記錄。如需詳細資訊，請參閱「AWS CloudTrail 使用者指南」**中的[使用 CloudTrail 事件歷史記錄](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/view-cloudtrail-events.html)。檢視**事件歷史記錄**不會產生 CloudTrail 費用。

如需 AWS 帳戶 過去 90 天內持續記錄的事件，請建立線索或 [CloudTrail Lake](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-lake.html) 事件資料存放區。

**CloudTrail 追蹤**  
*線索*能讓 CloudTrail 將日誌檔案交付至 Amazon S3 儲存貯體。使用 建立的所有線索 AWS 管理主控台 都是多區域。您可以使用 AWS CLI建立單一或多區域追蹤。建議您建立多區域追蹤，因為您擷取 AWS 區域 帳戶中所有 的活動。如果您建立單一區域追蹤，您只能檢視追蹤 AWS 區域中記錄的事件。如需追蹤的詳細資訊，請參閱《AWS CloudTrail 使用者指南》**中的[為您的 AWS 帳戶建立追蹤](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-create-and-update-a-trail.html)和[為組織建立追蹤](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/creating-trail-organization.html)。  
您可以透過建立追蹤，免費將持續管理事件的一個複本從 CloudTrail 傳遞至您的 Amazon S3 儲存貯體，但這樣做會產生 Amazon S3 儲存費用。如需 CloudTrail 定價的詳細資訊，請參閱 [AWS CloudTrail 定價](https://aws.amazon.com/cloudtrail/pricing/)。如需 Amazon S3 定價的相關資訊，請參閱 [Amazon S3 定價](https://aws.amazon.com/s3/pricing/)。

**CloudTrail Lake 事件資料存放區**  
*CloudTrail Lake* 讓您能夠對事件執行 SQL 型查詢。CloudTrail Lake 會將分列式 JSON 格式的現有事件轉換為 [Apache ORC](https://orc.apache.org/) 格式。ORC 是一種單欄式儲存格式，針對快速擷取資料進行了最佳化。系統會將事件彙總到*事件資料存放區*中，事件資料存放區是事件的不可變集合，其依據為您透過套用[進階事件選取器](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-lake-concepts.html#adv-event-selectors)選取的條件。套用於事件資料存放區的選取器控制哪些事件持續存在並可供您查詢。如需 CloudTrail Lake 的詳細資訊，請參閱*AWS CloudTrail 《 使用者指南*》中的[使用 AWS CloudTrail Lake](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-lake.html)。  
CloudTrail Lake 事件資料存放區和查詢會產生費用。建立事件資料存放區時，您可以選擇要用於事件資料存放區的[定價選項](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-lake-manage-costs.html#cloudtrail-lake-manage-costs-pricing-option)。此定價選項將決定擷取和儲存事件的成本，以及事件資料存放區的預設和最長保留期。如需 CloudTrail 定價的詳細資訊，請參閱 [AWS CloudTrail 定價](https://aws.amazon.com/cloudtrail/pricing/)。

## CloudTrail 中的 SNS 資料事件
<a name="cloudtrail-data-events"></a>

[資料事件](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/logging-data-events-with-cloudtrail.html#logging-data-events)提供在資源上或在資源中執行的資源操作的相關資訊 (例如，讀取或寫入 Amazon S3 物件)。這些也稱為資料平面操作。資料事件通常是大量資料的活動。根據預設，CloudTrail 不會記錄資料事件。CloudTrail **事件歷史記錄**不會記錄資料事件。

資料事件需支付額外的費用。如需 CloudTrail 定價的詳細資訊，請參閱 [AWS CloudTrail 定價](https://aws.amazon.com/cloudtrail/pricing/)。

您可以使用 CloudTrail 主控台 AWS CLI或 CloudTrail API 操作來記錄 SNS 資源類型的資料事件。如需如何記錄資料事件的詳細資訊，請參閱 *AWS CloudTrail 使用者指南*中的[使用 AWS 管理主控台　記錄資料事件](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/logging-data-events-with-cloudtrail.html#logging-data-events-console)和[使用 AWS Command Line Interface記錄資料事件](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/logging-data-events-with-cloudtrail.html#creating-data-event-selectors-with-the-AWS-CLI)。

下表列出您可以記錄資料事件的 SNS 資源類型。**資料事件類型 (主控台)** 資料行會顯示從 CloudTrail 主控台上的**資料事件類型**清單中選擇的值。**resources.type 值**欄會顯示值，您會在使用 AWS CLI 或 CloudTrail APIs 設定進階事件選取器時指定此`resources.type`值。**記錄到 CloudTrail 的資料 API** 資料行會針對資源類型顯示記錄到 CloudTrail 的 API 呼叫。


| 資料事件類型 (主控台) | resources.type 值 | 記錄到 CloudTrail 的資料 API | 
| --- | --- | --- | 
| SNS 主題 |  [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-sns-topic.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-sns-topic.html)  | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/sns/latest/dg/logging-using-cloudtrail.html) | 
| SNS 平台端點 |  AWS::SNS::PlatformEndpoint  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/sns/latest/dg/logging-using-cloudtrail.html)  | 

**注意**  
CloudTrail `AWS::SNS::PhoneNumber` 不會記錄 SNS 資源類型。

您可以設定進階事件選取器來篩選 `eventName`、`readOnly` 和 `resources.ARN` 欄位，以僅記錄對您重要的事件。如需這些欄位的詳細資訊，請參閱AWS CloudTrail API 參考**中的[https://docs.aws.amazon.com/awscloudtrail/latest/APIReference/API_AdvancedFieldSelector.html](https://docs.aws.amazon.com/awscloudtrail/latest/APIReference/API_AdvancedFieldSelector.html)。

如需有關記錄資料事件的資訊，請參閱《CloudTrail 使用者指南 AWS CLI 》中的使用 記錄資料事件 AWS 管理主控台 和使用 記錄資料事件。

## CloudTrail 中的 SNS 管理事件
<a name="cloudtrail-management-events"></a>

[管理事件](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/logging-management-events-with-cloudtrail.html#logging-management-events)提供有關在 資源上執行的管理操作的資訊 AWS 帳戶。這些也稱為控制平面操作。根據預設，CloudTrail 記錄管理事件。

AWS SNS 會將下列 SNS 控制平面操作記錄到 CloudTrail 做為*管理事件*。
+ `[AddPermission](https://docs.aws.amazon.com/sns/latest/api/API_AddPermission.html)`
+ `[CheckIfPhoneNumberIsOptedOut](https://docs.aws.amazon.com/sns/latest/api/API_CheckIfPhoneNumberIsOptedOut.html)`
+ `[ConfirmSubscription](https://docs.aws.amazon.com/sns/latest/api/API_ConfirmSubscription.html)`
+ `[CreatePlatformApplication](https://docs.aws.amazon.com/sns/latest/api/API_CreatePlatformApplication.html)`
+ `[CreatePlatformEndpoint](https://docs.aws.amazon.com/sns/latest/api/API_CreatePlatformEndpoint.html)`
+ `[CreateSMSSandboxPhoneNumber](https://docs.aws.amazon.com/sns/latest/api/API_CreateSMSSandboxPhoneNumber.html)`
+ `[CreateTopic](https://docs.aws.amazon.com/sns/latest/api/API_CreateTopic.html)`
+ `[DeleteEndpoint](https://docs.aws.amazon.com/sns/latest/api/API_DeleteEndpoint.html)`
+ `[DeletePlatformApplication](https://docs.aws.amazon.com/sns/latest/api/API_DeletePlatformApplication.html)`
+ `[DeleteSMSSandboxPhoneNumber](https://docs.aws.amazon.com/sns/latest/api/API_DeleteSMSSandboxPhoneNumber.html)`
+ `[DeleteTopic](https://docs.aws.amazon.com/sns/latest/api/API_DeleteTopic.html)`
+ `[GetDataProtectionPolicy](https://docs.aws.amazon.com/sns/latest/api/API_GetDataProtectionPolicy.html)`
+ `[GetEndpointAttributes](https://docs.aws.amazon.com/sns/latest/api/API_GetEndpointAttributes.html)`
+ `[GetPlatformApplicationAttributes](https://docs.aws.amazon.com/sns/latest/api/API_GetPlatformApplicationAttributes.html)`
+ `[GetSMSAttributes](https://docs.aws.amazon.com/sns/latest/api/API_GetSMSAttributes.html)`
+ `[GetSMSSandboxAccountStatus](https://docs.aws.amazon.com/sns/latest/api/API_GetSMSSandboxAccountStatus.html)`
+ `[GetSubscriptionAttributes](https://docs.aws.amazon.com/sns/latest/api/API_GetSubscriptionAttributes.html)`
+ `[GetTopicAttributes](https://docs.aws.amazon.com/sns/latest/api/API_GetTopicAttributes.html)`
+ `[ListEndpointsByPlatformApplication](https://docs.aws.amazon.com/sns/latest/api/API_ListEndpointsByPlatformApplication.html)`
+ `[ListOriginationNumbers](https://docs.aws.amazon.com/sns/latest/api/API_ListOriginationNumbers.html)`
+ `[ListPhoneNumbersOptedOut](https://docs.aws.amazon.com/sns/latest/api/API_ListPhoneNumbersOptedOut.html)`
+ `[ListPlatformApplications](https://docs.aws.amazon.com/sns/latest/api/API_ListPlatformApplications.html)`
+ `[ListSMSSandboxPhoneNumbers](https://docs.aws.amazon.com/sns/latest/api/API_ListSMSSandboxPhoneNumbers.html)`
+ `[ListSubscriptions](https://docs.aws.amazon.com/sns/latest/api/API_ListSubscriptions.html)`
+ `[ListSubscriptionsByTopic](https://docs.aws.amazon.com/sns/latest/api/API_ListSubscriptionsByTopic.html)`
+ `[ListTagsForResource](https://docs.aws.amazon.com/sns/latest/api/API_ListTagsForResource.html)`
+ `[ListTopics](https://docs.aws.amazon.com/sns/latest/api/API_ListTopics.html)`
+ `[OptInPhoneNumber](https://docs.aws.amazon.com/sns/latest/api/API_OptInPhoneNumber.html)`
+ `[PutDataProtectionPolicy](https://docs.aws.amazon.com/sns/latest/api/API_PutDataProtectionPolicy.html)`
+ `[RemovePermission](https://docs.aws.amazon.com/sns/latest/api/API_RemovePermission.html)`
+ `[SetEndpointAttributes](https://docs.aws.amazon.com/sns/latest/api/API_SetEndpointAttributes.html)`
+ `[SetPlatformApplicationAttributes](https://docs.aws.amazon.com/sns/latest/api/API_SetPlatformApplicationAttributes.html)`
+ `[SetSMSAttributes](https://docs.aws.amazon.com/sns/latest/api/API_SetSMSAttributes.html)`
+ `[SetSubscriptionAttributes](https://docs.aws.amazon.com/sns/latest/api/API_SetSubscriptionAttributes.html)`
+ `[SetTopicAttributes](https://docs.aws.amazon.com/sns/latest/api/API_SetTopicAttributes.html)`
+ `[Subscribe](https://docs.aws.amazon.com/sns/latest/api/API_Subscribe.html)`
+ `[TagResource](https://docs.aws.amazon.com/sns/latest/api/API_TagResource.html)`
+ `[Unsubscribe](https://docs.aws.amazon.com/sns/latest/api/API_Unsubscribe.html)`
+ `[UntagResource](https://docs.aws.amazon.com/sns/latest/api/API_UntagResource.html)`
+ `[VerifySMSSandboxPhoneNumber](https://docs.aws.amazon.com/sns/latest/api/API_VerifySMSSandboxPhoneNumber.html)`

**注意**  
當您未登入 Amazon Web Services （未驗證模式） 並叫用 [https://docs.aws.amazon.com/sns/latest/api/API_ConfirmSubscription.html](https://docs.aws.amazon.com/sns/latest/api/API_ConfirmSubscription.html)或 [https://docs.aws.amazon.com/sns/latest/api/API_Unsubscribe.html](https://docs.aws.amazon.com/sns/latest/api/API_Unsubscribe.html)動作時，它們將不會記錄到 CloudTrail。例如，當您選擇電子郵件通知提供的連結，以確認對某一主題的等待訂閱時，系統會以未驗證模式叫用 `ConfirmSubscription` 動作。在此範例中，`ConfirmSubscription` 動作不會記錄到 CloudTrail 中。

## SNS 事件範例
<a name="cloudtrail-event-examples"></a>

一個事件代表任何來源提出的單一請求，並包含請求 API 操作的相關資訊、操作的日期和時間、請求參數等。CloudTrail 日誌檔案不是公有 API 呼叫的已排序堆疊追蹤，因此事件不會以任何特定順序顯示。

下列範例顯示示範 **`ListTopics`**、 `CreateTopic`和 `DeleteTopic`動作的 CloudTrail 事件。

```
{
  "Records": [
    {
      "eventVersion": "1.02",
      "userIdentity": {
        "type": "IAMUser",
        "userName": "Bob",
        "principalId": "EX_PRINCIPAL_ID",
        "arn": "arn:aws:iam::123456789012:user/Bob",
        "accountId": "123456789012",
        "accessKeyId": "AKIAIOSFODNN7EXAMPLE"
      },
      "eventTime": "2014-09-30T00:00:00Z",
      "eventSource": "sns.amazonaws.com",
      "eventName": "ListTopics",
      "awsRegion": "us-west-2",
      "sourceIPAddress": "127.0.0.1",
      "userAgent": "aws-sdk-java/unknown-version",
      "requestParameters": {
        "nextToken": "ABCDEF1234567890EXAMPLE=="
      },
      "responseElements": null,
      "requestID": "example1-b9bb-50fa-abdb-80f274981d60",
      "eventID": "example0-09a3-47d6-a810-c5f9fd2534fe",
      "eventType": "AwsApiCall",
      "recipientAccountId": "123456789012"
    },
    {
      "eventVersion": "1.02",
      "userIdentity": {
        "type": "IAMUser",
        "userName": "Bob",
        "principalId": "EX_PRINCIPAL_ID",
        "arn": "arn:aws:iam::123456789012:user/Bob",
        "accountId": "123456789012",
        "accessKeyId": "AKIAIOSFODNN7EXAMPLE"
      },
      "eventTime": "2014-09-30T00:00:00Z",
      "eventSource": "sns.amazonaws.com",
      "eventName": "CreateTopic",
      "awsRegion": "us-west-2",
      "sourceIPAddress": "127.0.0.1",
      "userAgent": "aws-sdk-java/unknown-version",
      "requestParameters": {
        "name": "hello"
      },
      "responseElements": {
        "topicArn": "arn:aws:sns:us-west-2:123456789012:hello-topic"
      },
      "requestID": "example7-5cd3-5323-8a00-f1889011fee9",
      "eventID": "examplec-4f2f-4625-8378-130ac89660b1",
      "eventType": "AwsApiCall",
      "recipientAccountId": "123456789012"
    },
    {
      "eventVersion": "1.02",
      "userIdentity": {
        "type": "IAMUser",
        "userName": "Bob",
        "principalId": "EX_PRINCIPAL_ID",
        "arn": "arn:aws:iam::123456789012:user/Bob",
        "accountId": "123456789012",
        "accessKeyId": "AKIAIOSFODNN7EXAMPLE"
      },
      "eventTime": "2014-09-30T00:00:00Z",
      "eventSource": "sns.amazonaws.com",
      "eventName": "DeleteTopic",
      "awsRegion": "us-west-2",
      "sourceIPAddress": "127.0.0.1",
      "userAgent": "aws-sdk-java/unknown-version",
      "requestParameters": {
        "topicArn": "arn:aws:sns:us-west-2:123456789012:hello-topic"
      },
      "responseElements": null,
      "requestID": "example5-4faa-51d5-aab2-803a8294388d",
      "eventID": "example8-6443-4b4d-abfd-1b867280d964",
      "eventType": "AwsApiCall",
      "recipientAccountId": "123456789012"
    }
  ]
}
```

以下範例顯示了展示 `Publish` 動作的 CloudTrail 事件。

```
{
  "eventVersion": "1.09",
  "userIdentity": {
    "type": "AssumedRole",
    "principalId": "EX_PRINCIPAL_ID",
    "arn": "arn:aws:iam::123456789012:user/Bob",
    "accountId": "123456789012",
    "accessKeyId": "AKIAIOSFODNN7EXAMPLE",
    "sessionContext": {
      "sessionIssuer": {
        "type": "Role",
        "principalId": "AKIAIOSFODNN7EXAMPLE",
        "arn": "arn:aws:iam::123456789012:role/Admin",
        "accountId": "123456789012",
        "userName": "ExampleUser"
      },
      "attributes": {
        "creationDate": "2023-08-21T16:44:05Z",
        "mfaAuthenticated": "false"
      }
    }
  },
  "eventTime": "2023-08-21T16:48:37Z",
  "eventSource": "sns.amazonaws.com",
  "eventName": "Publish",
  "awsRegion": "us-east-1",
  "sourceIPAddress": "192.0.2.0",
  "userAgent": "aws-cli/1.29.16 md/Botocore#1.31.16 ua/2.0 os/linux#5.4.250-173.369.amzn2int.x86_64 md/arch#x86_64 lang/python#3.8.17 md/pyimpl#CPython cfg/retry-mode#legacy botocore/1.31.16",
  "requestParameters": {
    "topicArn": "arn:aws:sns:us-east-1:123456789012:ExampleSNSTopic",
    "message": "HIDDEN_DUE_TO_SECURITY_REASONS",
    "subject": "HIDDEN_DUE_TO_SECURITY_REASONS",
    "messageStructure": "json",
    "messageAttributes": "HIDDEN_DUE_TO_SECURITY_REASONS"
  },
  "responseElements": {
    "messageId": "0787cd1e-d92b-521c-a8b4-90434e8ef840"
  },
  "requestID": "0a8ab208-11bf-5e01-bd2d-ef55861b545d",
  "eventID": "bb3496d4-5252-4660-9c28-3c6aebdb21c0",
  "readOnly": false,
  "resources": [
    {
      "accountId": "123456789012",
      "type": "AWS::SNS::Topic",
      "ARN": "arn:aws:sns:us-east-1:123456789012:ExampleSNSTopic"
    }
  ],
  "eventType": "AwsApiCall",
  "managementEvent": false,
  "recipientAccountId": "123456789012",
  "eventCategory": "Data",
  "tlsDetails": {
    "tlsVersion": "TLSv1.2",
    "cipherSuite": "ECDHE-RSA-AES128-GCM-SHA256",
    "clientProvidedHostHeader": "sns.us-east-1.amazonaws.com"
  }
}
```

以下範例顯示了展示 `PublishBatch` 動作的 CloudTrail 事件。

```
{
  "eventVersion": "1.09",
  "userIdentity": {
    "type": "AssumedRole",
    "principalId": "EX_PRINCIPAL_ID",
    "arn": "arn:aws:iam::123456789012:user/Bob",
    "accountId": "123456789012",
    "accessKeyId": "AKIAIOSFODNN7EXAMPLE",
    "sessionContext": {
      "sessionIssuer": {
        "type": "Role",
        "principalId": "AKIAIOSFODNN7EXAMPLE",
        "arn": "arn:aws:iam::123456789012:role/Admin",
        "accountId": "123456789012",
        "userName": "ExampleUser"
      },
      "attributes": {
        "creationDate": "2023-08-21T19:20:49Z",
        "mfaAuthenticated": "false"
      }
    }
  },
  "eventTime": "2023-08-21T19:22:01Z",
  "eventSource": "sns.amazonaws.com",
  "eventName": "PublishBatch",
  "awsRegion": "us-east-1",
  "sourceIPAddress": "192.0.2.0",
  "userAgent": "aws-cli/1.29.16 md/Botocore#1.31.16 ua/2.0 os/linux#5.4.250-173.369.amzn2int.x86_64 md/arch#x86_64 lang/python#3.8.17 md/pyimpl#CPython cfg/retry-mode#legacy botocore/1.31.16",
  "requestParameters": {
    "topicArn": "arn:aws:sns:us-east-1:123456789012:ExampleSNSTopic",
    "publishBatchRequestEntries": [
      {
        "id": "1",
        "message": "HIDDEN_DUE_TO_SECURITY_REASONS"
      },
      {
        "id": "2",
        "message": "HIDDEN_DUE_TO_SECURITY_REASONS"
      }
    ]
  },
  "responseElements": {
    "successful": [
      {
        "id": "1",
        "messageId": "30d68101-a64a-5573-9e10-dc5c1dd3af2f"
      },
      {
        "id": "2",
        "messageId": "c0aa0c5c-561d-5455-b6c4-5101ed84de09"
      }
    ],
    "failed": []
  },
  "requestID": "e2cdf7f3-1b35-58ad-ac9e-aaaea0ace2f1",
  "eventID": "10da9a14-0154-4ab6-b3a5-1825b229a7ed",
  "readOnly": false,
  "resources": [
    {
      "accountId": "123456789012",
      "type": "AWS::SNS::Topic",
      "ARN": "arn:aws:sns:us-east-1:123456789012:ExampleSNSTopic"
    }
  ],
  "eventType": "AwsApiCall",
  "managementEvent": false,
  "recipientAccountId": "123456789012",
  "eventCategory": "Data",
  "tlsDetails": {
    "tlsVersion": "TLSv1.2",
    "cipherSuite": "ECDHE-RSA-AES128-GCM-SHA256",
    "clientProvidedHostHeader": "sns.us-east-1.amazonaws.com"
  }
}
```

如需有關 CloudTrail 記錄內容的資訊，請參閱《AWS CloudTrail 使用者指南》**中的 [CloudTrail record contents](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-event-reference-record-contents.html)。

# 使用 Amazon CloudWatch 監控 Amazon SNS 主題
<a name="sns-monitoring-using-cloudwatch"></a>

Amazon SNS 和 Amazon CloudWatch 已經整合，讓您可收集、檢視和分析每個作用中 Amazon SNS 通知的指標。一旦您為 Amazon SNS 設定好 CloudWatch 之後，就可以對於您的 Amazon SNS 主題、推送通知和簡訊傳送獲得更深入的效能洞察。例如，您可以設定警示，在達到 Amazon SNS 指標的指定閾值 (例如 `NumberOfNotificationsFailed`) 時傳送電子郵件通知。有關 Amazon SNS 傳送至 CloudWatch 之所有指標的清單，請參閱 [Amazon SNS 指標](#sns-metrics)。如需使用 Amazon SNS 推送通知的詳細資訊，請參閱 [使用 Amazon SNS 傳送行動推播通知](sns-mobile-application-as-subscriber.md)。

**注意**  
自動收集您為 Amazon SNS 主題設定 CloudWatch 的指標，並在 *1 分鐘*間隔推送到 CloudWatch。這些指標會針對所有符合 CloudWatch 作用中的要求的主題進行收集。被 CloudWatch 視為作用中的主題為自上次在主題上活動 (例如任何 API 呼叫) 已長達六小時者。  
CloudWatch 中所報告的 Amazon SNS 指標不會產生費用；這是做為 Amazon SNS 服務的一部分提供。

## 檢視 Amazon SNS 的 CloudWatch 指標
<a name="view-cloudwatch-metrics"></a>

您可以使用 CloudWatch 主控台、CloudWatch 本身的命令列界面 (CLI) 或使用 CloudWatch API 的程式設計方式來監控 Amazon SNS 指標。下列程序顯示如何使用 AWS 管理主控台存取指標。

**使用 CloudWatch 主控台檢視指標**

1. 登入 [CloudWatch 主控台](https://console.aws.amazon.com/cloudwatch)。

1. 在導覽面板上，選擇 **Metrics (指標)**。

1. 在 **All metrics** (所有指標) 標籤上，選擇 **SNS**，然後選擇下列其中一個維度：
   + **Country, SMS Type (國家/地區，簡訊類型)**
   + **PhoneNumber**
   + **Topic Metrics (主題指標)**
   + **Metrics with no dimensions (無維度的指標)**

1. 若要檢視更多詳細資訊，請選擇特定項目。例如，如果您選擇 **Topic Metrics** (主題指標)，然後選擇 **NumberOfMessagesPublished**，則會顯示 6 小時範圍內一分鐘時間所發佈 Amazon SNS 訊息的平均數量。

1. 若要檢視 Amazon SNS 用量指標，請在 **All metrics** (所有指標) 索引標籤上選擇 **Usage** (用量)，然後選取 **target Amazon SNS usage metric** (針對 Amazon SNS 用量指標) (例如 `NumberOfMessagesPublishedPerAccount`)。

## 設定 Amazon SNS 指標的 CloudWatch 警示
<a name="SNS_AlarmMetrics"></a>

CloudWatch 亦可讓您設定到達指標的閾值時的警示。例如，您可以設定指標 **NumberOfNotificationsFailed** 的警示，以便在抽樣週期內到達指定的閾值時，傳送電子郵件通知給您通知您發生事件。

**使用 CloudWatch 主控台設定警示**

1. 登入 AWS 管理主控台 ，並在 https：//[https://console.aws.amazon.com/cloudwatch/](https://console.aws.amazon.com/cloudwatch/) 開啟 CloudWatch 主控台。

1.  選擇 **Alarms** (警示)，然後選擇 **Create Alarm** (建立警示) 按鈕。這會啟動 **Create Alarm** (建立警示) 精靈。

1.  捲動 Amazon SNS 指標以找到您要設定警示的指標。選取要建立警示的指標，然後選擇 **Continue** (繼續)。

1. 填入指標的 **Name** (名稱)、**Description** (描述)、**Threshold** (閾值) 和 **Time** (時間) 值，然後選擇 **Continue** (繼續)。

1. 選擇 **Alarm** (警示) 做為警示狀態。如果您希望 CloudWatch 在達到警示狀態時傳送電子郵件給您，請選擇現有的 Amazon SNS 主題或選擇 **Create New Email Topic** (建立新的電子郵件主題)。如果您選擇 **Create New Email Topic** (建立新的電子郵件主題)，即可為新的主題設定名稱和電子郵件地址。此清單將會儲存並顯示在下拉式方塊中，供未來警示用。選擇 **Continue** (繼續)。
**注意**  
如果您使用 **Create New Email Topic** (建立新的電子郵件主題) 來建立新的 Amazon SNS 主題，電子郵件地址必須先經過驗證才會接收通知。電子郵件只有在警示進入警示狀態時才會傳送。如果此警示狀態在驗證電子郵件地址之前發生變更，就不會收到通知。

1. 此時，**Create Alarm** (建立警示) 精靈會提供您機會檢閱您將建立的警示。如果您需要進行任何變更，可以使用右邊的 **Edit** (編輯) 連結。編輯好之後，選擇 **Create Alarm** (建立警示)。

如需使用 CloudWatch 和警示的詳細資訊，請參閱 [CloudWatch 文件](https://aws.amazon.com/documentation/cloudwatch)。

## Amazon SNS 指標
<a name="sns-metrics"></a>

Amazon SNS 會傳送下列指標至 CloudWatch。


| 命名空間 | 指標 | Description | 
| --- | --- | --- | 
| AWS/SNS |  NumberOfMessagesPublished  |  發佈至 Amazon SNS 主題的訊息數量。 **單位：**計數 **有效維度：**Application、 PhoneNumber、 Platform 和 TopicName **有效的統計資訊：**總和  | 
| AWS/SNS |  NumberOfNotificationsDelivered  |  成功從 Amazon SNS 主題傳送至訂閱端點的訊息數量。 為了讓傳遞嘗試成功，端點的訂閱必須接受訊息。訂閱接受訊息的情況為 a.) 它缺少篩選條件政策或 b.) 其篩選條件政策包含符合指派給該訊息的屬性。如果訂閱拒絕訊息，此指標不會計入傳遞嘗試次數。 **單位：**計數 **有效維度：**Application、 PhoneNumber、 Platform 和 TopicName **有效的統計資訊：**總和  | 
| AWS/SNS |  NumberOfNotificationsFailed  |  Amazon SNS 無法傳送之訊息的數量。 對於 Amazon SQS、電子郵件、簡訊或行動推送端點，當 Amazon SNS 停止嘗試傳遞訊息，此指標會以 1 的方式遞增。對於 HTTP 或 HTTPS 端點，指標包含每個失敗的傳遞嘗試，包括首次嘗試之後的重試次數。對於所有其他端點，計數會在訊息傳遞失敗時增加 1 (不論嘗試次數多寡)。 這個指標不包含被訂閱篩選政策拒絕的訊息。 您可以控制 HTTP 端點的重試數目。如需詳細資訊，請參閱[Amazon SNS 訊息傳遞重試](sns-message-delivery-retries.md)。 **單位：**計數 **有效維度：**Application、 PhoneNumber、 Platform 和 TopicName **有效統計資料：**總和、平均  | 
| AWS/SNS | NumberOfNotificationsFilteredOut |  被訂閱篩選政策拒絕的訊息數。當訊息屬性不符政策屬性時，篩選政策會拒絕訊息。 **單位：**計數 **有效維度：**Application、 PhoneNumber、 Platform 和 TopicName **有效統計資料：**總和、平均  | 
| AWS/SNS | NumberOfNotificationsFilteredOut-MessageAttributes | 被以屬性為基礎篩選的訂閱篩選政策拒絕的訊息數。 **單位：**計數 **有效維度：**Application、 PhoneNumber、 Platform 和 TopicName **有效統計資料：**總和、平均  | 
| AWS/SNS | NumberOfNotificationsFilteredOut-MessageBody |  被以承載為基礎篩選的訂閱篩選政策拒絕的訊息數。 **單位：**計數 **有效維度：**Application、 PhoneNumber、 Platform 和 TopicName **有效統計資料：**總和、平均  | 
| AWS/SNS | NumberOfNotificationsFilteredOut-InvalidAttributes |  因為訊息屬性無效而被訂閱篩選條件政策拒絕的訊息數量，例如，因為屬性 JSON 的格式不正確。 **單位：**計數 **有效維度：**Application、 PhoneNumber、 Platform 和 TopicName **有效統計資料：**總和、平均  | 
| AWS/SNS | NumberOfNotificationsFilteredOut-NoMessageAttributes |  因為訊息沒有屬性而被訂閱篩選條件政策拒絕的訊息數量。 **單位：**計數 **有效維度：**Application、 PhoneNumber、 Platform 和 TopicName **有效統計資料：**總和、平均  | 
| AWS/SNS | NumberOfNotificationsFilteredOut-InvalidMessageBody |  因訊息內文無效而被訂閱篩選政策拒絕的訊息數，例如，JSON 訊息內文無效。 **單位：**計數 **有效維度：**Application、 PhoneNumber、 Platform 和 TopicName **有效統計資料：**總和、平均  | 
| AWS/SNS |  NumberOfNotificationsRedrivenToDlq  |  已移至無效字母佇列的訊息數量。 **單位：**計數 **有效維度：**Application、 PhoneNumber、 Platform 和 TopicName **有效統計資料：**總和、平均 | 
| AWS/SNS |  NumberOfNotificationsFailedToRedriveToDlq  |  無法移至無效字母佇列的訊息數量。 **單位：**計數 **有效維度：**Application、 PhoneNumber、 Platform 和 TopicName **有效統計資料：**總和、平均 | 
| AWS/SNS |  PublishSize  |  已發佈訊息的大小。 **單位：**位元組 **有效維度：**Application、 PhoneNumber、 Platform 和 TopicName **有效統計資料：**最小值、最大值、平均值和計數  | 
| AWS/SNS | SMSMonthToDateSpentUSD |  您從當月月初開始傳送簡訊所累計的費用。 您可以為此指標設定警示，以得知本月至今的費用是否已接近您帳戶的每月簡訊費用配額。當 Amazon SNS 判定傳送簡訊會致使成本超過此配額時，就會在幾分鐘內停止發步簡訊。 如需有關每月簡訊費用配額的設定資訊，或者要求 AWS提高費用配額的相關資訊，請參閱 [在 Amazon SNS 中設定簡訊偏好設定](sms_preferences.md)。 **單位：**USD **有效維度：**無 **有效的統計資訊：**總和  | 
| AWS/SNS |  SMSSuccessRate  |  成功傳送簡訊的比率。 **單位：**計數 **有效維度：**PhoneNumber **有效統計資料：**總和、平均、資料範例  | 

## Amazon SNS 指標的維度
<a name="sns-metric-dimensions"></a>

Amazon Simple Notification Service 會傳送下列維度至 CloudWatch。


|  維度  |  Description  | 
| --- | --- | 
|  Application  |  篩選應用程式物件，該物件代表向其中一個支援的推送通知服務 (例如 APN 和 FCM) 註冊的應用程式和裝置。  | 
|  Application,Platform  |  篩選應用程式和平台物件，其中平台物件適用於支援的推送通知服務，例如 APN 和 FCM。  | 
| Country |  篩選簡訊的目的地國家/地區或區域。國家/地區或區域是以其 ISO 3166-1 alpha-2 代碼表示。  | 
|  PhoneNumber  |  當您直接將簡訊發佈至電話號碼 (不含主題) 時，篩選電話號碼。  | 
|  Platform  |  篩選適用於推送通知服務 (例如 APN 和 FCM) 的平台物件。  | 
|  TopicName  |  篩選 Amazon SNS 主題名稱。  | 
| SMSType |  篩選簡訊的訊息類型。可以是*促銷型*或*交易型*。  | 

## Amazon SNS 用量指標
<a name="sns-usage-metrics"></a>

Amazon Simple Notification Service 會傳送下列用量指標至 CloudWatch。


|  命名空間  |  服務  | 指標 | 資源 | 類型 | 說明 | 
| --- | --- | --- | --- | --- | --- | 
| AWS/用量 | SNS | ResourceCount | NumberOfMessagesPublishedPerAccount | 資源 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/sns/latest/dg/sns-monitoring-using-cloudwatch.html)  | 
| AWS/用量 | SNS | ResourceCount | ApproximateNumberOfTopics | 資源 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/sns/latest/dg/sns-monitoring-using-cloudwatch.html)  | 
| AWS/用量 | SNS | ResourceCount | ApproximateNumberOfFilterPolicies | 資源 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/sns/latest/dg/sns-monitoring-using-cloudwatch.html)  | 
| AWS/用量 | SNS | ResourceCount | ApproximateNumberOfPendingSubscriptions | 資源 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/sns/latest/dg/sns-monitoring-using-cloudwatch.html)  | 
| AWS/用量 | SNS | CallCount |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/sns/latest/dg/sns-monitoring-using-cloudwatch.html)  | API |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/sns/latest/dg/sns-monitoring-using-cloudwatch.html)  | 