

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 创建通知规则
<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 管理控制台 并打开 CodeDeploy 控制台，网址为[https://console.aws.amazon.com/codedeploy/](https://console.aws.amazon.com/codedeploy/)。

1. 选择 **Application（应用程序）**，然后选择要在其中添加通知的应用程序。

1. 在应用程序页面上，选择 **Notify（通知）**，然后选择 **Create notification rule（创建通知规则）**。您也可以转到应用程序的 **Settings（设置）**页面，然后选择 **Create notification rule（创建通知规则）**。

1. 在 **Notification name（通知名称）**中，输入规则的名称。

1. 如果您只想在通知中 EventBridge 包含提供给 Amazon 的信息，请在 “**详情类型**” 中选择 “**基本**”。如果您想包括提供给 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_cn/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 开发者版之间的集成，以将通知发送到 Amazon Chime 聊天室或 Slack 频道。有关更多信息，请参阅[在聊天应用程序中配置通知与 Amazon Q 开发者版之间的集成](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 *123456789012* 的 AWS 账户*MyDeploymentApplication*中名**MyNotificationRule**为的应用程序的通知规则。通知将以完整详细信息类型发送到名为 “部署成功*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"
   }
   ```