

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

# 設定通知的 Amazon SNS 主題
<a name="set-up-sns"></a>

設定通知最簡單方法是，在建立通知規則時建立 Amazon SNS 主題。如果現有 Amazon SNS 主題符合下列要求，您可以使用該主題：
+ 它在與您要為其建立通知規則的資源 （建置專案、部署應用程式、儲存庫或管道） AWS 區域 相同的 中建立。
+ 該主題在 2019 年 11 月 5 日之前未用於傳送 CodeCommit 的通知。如果已使用，則其將包含啟用該功能時的政策陳述式。您可以選擇使用此主題，但是您將必須依照前面程序規定來加入其他的政策。如果在 2019 年 11 月 5 日之前，仍有一或多個儲存庫設定用於通知，則現有的政策陳述式即不應移除。
+ 它具有允許 AWS CodeStar Notifications 將通知發佈到主題的政策。<a name="set-up-sns-procedure"></a>

**設定 Amazon SNS 主題做為 AWS CodeStar Notifications 通知規則的目標**

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

1. 在導覽列中，選擇 **Topics (主題)**、選擇您要設定的主題，然後選擇 **Edit (編輯)**。

1. 展開 **Access policy (存取政策)**，然後選擇 **Advanced (進階)**。

1. 在 JSON 編輯器中，為政策新增下列陳述式。包含主題 ARN、、 AWS 帳戶 ID AWS 區域和主題名稱。

   ```
   	{
         "Sid": "AWSCodeStarNotifications_publish",
         "Effect": "Allow",
         "Principal": {
           "Service": [
             "codestar-notifications.amazonaws.com"
           ]
         },
         "Action": "SNS:Publish",
         "Resource": "arn:aws:sns:{{us-east-2}}:{{123456789012}}:{{codestar-notifications-MyTopicForNotificationRules}}"
       }
   ```

   該政策陳述式應如以下範例所示。

   ```
   {
     "Version": "2008-10-17",		 	 	 
     "Id": "__default_policy_ID",
     "Statement": [
       {
         "Sid": "__default_statement_ID",
         "Effect": "Allow",
         "Principal": {
           "AWS": "*"
         },
         "Action": [
           "SNS:GetTopicAttributes",
           "SNS:SetTopicAttributes",
           "SNS:AddPermission",
           "SNS:RemovePermission",
           "SNS:DeleteTopic",
           "SNS:Subscribe",
           "SNS:ListSubscriptionsByTopic",
           "SNS:Publish"
         ],
         "Resource": "arn:aws:sns:{{us-east-2}}:{{123456789012}}:{{codestar-notifications-MyTopicForNotificationRules}}",
         "Condition": {
           "StringEquals": {
             "AWS:SourceOwner": "123456789012"
           }
         }
       },
   	{
         "Sid": "AWSCodeStarNotifications_publish",
         "Effect": "Allow",
         "Principal": {
           "Service": [
             "codestar-notifications.amazonaws.com"
           ]
         },
         "Action": "SNS:Publish",
         "Resource": "arn:aws:sns:{{us-east-2}}:{{123456789012}}:{{codestar-notifications-MyTopicForNotificationRules}}"
       }
     ]
   }
   ```

1. 選擇**儲存變更**。

1. 如果您想要使用加密的 Amazon SNS 主題來傳送通知，您還必須將下列陳述 AWS KMS式新增至 政策，以啟用事件來源 (AWS CodeStar Notifications) 與加密主題之間的相容性 AWS KMS key。將 AWS 區域 （在此範例中為 us-east-2) 取代為建立金鑰 AWS 區域 的 。

------
#### [ JSON ]

****  

   ```
   {
       "Version":"2012-10-17",		 	 	 
       "Statement": [
           {
               "Effect": "Allow",
               "Principal": {
                   "Service": "codestar-notifications.amazonaws.com"
               },
               "Action": [
                   "kms:GenerateDataKey*",
                   "kms:Decrypt"
               ],
               "Resource": "*",
               "Condition": {
                   "StringEquals": {
                       "kms:ViaService": "sns.us-east-2.amazonaws.com"
                   }
               }
           }
       ]
   }
   ```

------

   如需詳細資訊，請參閱「*AWS Key Management Service 開發人員指南*中的[靜態加密](https://docs.aws.amazon.com/kms/latest/developerguide/sns-server-side-encryption.html#sns-what-permissions-for-sse)和[搭配 AWS KMS使用政策條件](https://docs.aws.amazon.com/kms/latest/developerguide/policy-conditions.html)。