

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

# 建立通知規則
<a name="notification-rule-create"></a>

您可以使用通知規則，在部署應用程式發生變更時通知使用者，例如部署成功和部署失敗。通知規則會同時指定用於傳送通知的事件和 Amazon SNS 主題。如需詳細資訊，請參閱[什麼是通知？](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/welcome.html)

您可以使用 主控台或 AWS CLI 來建立 的通知規則 AWS CodeDeploy。<a name="notification-rule-create-console"></a>

# 建立通知規則 (主控台)
<a name="notification-rule-create-console"></a>

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

1. 選擇 **Application (應用程式)**，然後選擇要新增通知的應用程式。

1. 在應用程式頁面上，選擇 **Notify (通知)**，然後選擇 **Create notification rule (建立通知規則)**。您也可以移至應用程式的 **Settings (設定)** 頁面，然後選擇 **Create notification rule (建立通知規則)**。

1. 在 **Notification name (通知名稱)** 中，輸入規則的名稱。

1. 如果您只希望提供給 Amazon EventBridge 的資訊包含在通知中，請在 **Detail type (詳細資訊類型)** 中，選擇 **Basic (基本)**。如果您想要包含提供給 Amazon EventBridge 的資訊，以及 CodeDeploy 或通知管理員可能提供的資訊，請選擇**完整**。

   如需詳細資訊，請參閱[了解通知內容和安全性](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/security.html#security-notifications)。

1.  在 **Events that trigger notifications (觸發通知的事件)** 中，選取您要傳送通知的事件。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/codedeploy/latest/userguide/notification-rule-create.html)

1. 在 **Targets (目標)** 中，選擇 **Create SNS topic (建立 SNS 主題)**。
**注意**  
當您建立主題時，會為您套用允許 CodeDeploy 發佈事件至主題的政策。使用專為 CodeDeploy 通知建立的主題，也有助於確保您只將使用者新增至要查看此部署應用程式相關通知之主題的訂閱清單。

   在 **codestar-notifications-** 字首之後，輸入主題的名稱，然後選擇 **Submit (提交)**。
**注意**  
如果您要使用現有 Amazon SNS 主題而非建立新主題，請在 **Targets (目標)** 中選擇其 ARN。請確定主題具有適當的存取政策，而且訂閱者清單只包含允許查看部署應用程式相關資訊的使用者。如需詳細資訊，請參閱[設定通知的現有 Amazon SNS 主題](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/set-up-sns.html)和[了解通知內容和安全性](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/security.html#security-notifications)。

1. 若要完成建立規則，請選擇 **Submit (提交)**。

1. 您必須為使用者訂閱規則的 Amazon SNS 主題，才能接收通知。如需詳細資訊，請參閱[訂閱使用者成為目標的 Amazon SNS 主題](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/subscribe-users-sns.html)。您也可以在聊天應用程式中設定通知與 Amazon Q Developer 之間的整合，將通知傳送至 Amazon Chime 聊天室或 Slack 頻道。如需詳細資訊，請參閱[在聊天應用程式中設定通知與 Amazon Q Developer 之間的整合](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/notifications-chatbot.html)。<a name="notification-rule-create-cli"></a>

# 建立通知規則 (AWS CLI)
<a name="notification-rule-create-cli"></a>

1. 在終端機或命令提示字元中，執行 **create-notification rule** 命令以產生 JSON 架構：

   ```
   aws codestar-notifications create-notification-rule --generate-cli-skeleton > rule.json
   ```

   您可以將檔案命名為任何您想要的名稱。在此範例中，檔案命名為 *rule.json*。

1. 在純文字編輯器中開啟 JSON 檔案，並進行編輯，以包含規則所需的資源、事件類型和 Amazon SNS 目標。下列範例顯示 ID **MyNotificationRule** 為 *123456789012* AWS 之帳戶中名為 *MyDeploymentApplication* 之應用程式的通知規則。部署成功時，通知會以完整詳細資訊類型傳送至名為 *codestar-notifications-MyNotificationTopic* 的 Amazon SNS 主題：

   ```
   {
       "Name": "MyNotificationRule",
       "EventTypeIds": [
           "codedeploy-application-deployment-succeeded"
       ],
       "Resource": "arn:aws:codebuild:us-east-2:123456789012:MyDeploymentApplication",
       "Targets": [
           {
               "TargetType": "SNS",
               "TargetAddress": "arn:aws:sns:us-east-2:123456789012:codestar-notifications-MyNotificationTopic"
           }
       ],
       "Status": "ENABLED",
       "DetailType": "FULL"
   }
   ```

   儲存檔案。

1. 在終端機或命令列中，再次執行 **create-notification-rule** 命令，使用您剛編輯的檔案建立通知規則：

   ```
   aws codestar-notifications create-notification-rule --cli-input-json  file://rule.json
   ```

1. 如果成功，此命令會傳回通知規則的 ARN，如下所示：

   ```
   {
       "Arn": "arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE"
   }
   ```