

# Working with notification rule targets
Working with notification rule targets

A notification rule target is a destination that defines where you want notifications to be sent when a notification rule's event conditions are met. You can choose between Amazon SNS topics and AWS Chatbot clients that are configured for Slack or Microsoft Teams channels. You can create an Amazon SNS topic as a target as part of creating a notification rule (recommended). You can also choose an existing Amazon SNS topic in the same AWS Region as the notification rule, but you must configure it with the required policy. If you choose to use an AWS Chatbot client as a target, you must first create that client in AWS Chatbot. 

If you want to extend the reach of notifications, you can manually configure integration between notifications and AWS Chatbot so that notifications are sent to Amazon Chime chatrooms. You can then choose the Amazon SNS topic configured for that AWS Chatbot client as the target for the notification rule. For more information, see [To integrate notifications with AWS Chatbot and Amazon Chime](notifications-chatbot.md#notifications-chatbot-chime).

You can use the Developer Tools console or the AWS CLI to manage notification targets. You can use the console or the AWS CLI to create and configure Amazon SNS topics and AWS Chatbot clients as [targets](concepts.md#targets). You can also configure integration between the Amazon SNS topics that you configure as targets and AWS Chatbot. This makes it possible for you to send notifications to Amazon Chime chatrooms. For more information, see [Configure integration between notifications and AWS Chatbot](notifications-chatbot.md).

**Topics**
+ [

# Create or configure a notification rule target
](notification-target-create.md)
+ [

# View notification rule targets
](notification-target-view.md)
+ [

# Add or remove a target for a notification rule
](notification-target-change-rule.md)
+ [

# Delete a notification rule target
](notification-target-delete.md)

# Create or configure a notification rule target


Notification rule targets are Amazon SNS topics or AWS Chatbot clients configured for Slack or Microsoft Teams channels. 

An AWS Chatbot client must be created before you can select a client as a target. When you choose an AWS Chatbot client as a target for a notification rule, an Amazon SNS topic is configured for that AWS Chatbot client with all the policies required for notifications to be sent to the Slack or Microsoft Teams channel. You don't have to configure any existing Amazon SNS topics for the AWS Chatbot client.

You can create Amazon SNS notification rule targets in the Developer Tools console when you create a notification rule. The policy that allows notifications to be sent to that topic is applied for you. This is the easiest way to create a target for a notification rule. For more information, see [Create a notification rule](notification-rule-create.md). 

If you use an existing Amazon SNS topic, you must configure it with an access policy that allows the resource to send notifications to that topic. For an example, see [Configure Amazon SNS topics for notifications](set-up-sns.md).

**Note**  
If you want to use an existing Amazon SNS topic instead of creating a new one, in **Targets**, choose its ARN. Make sure the topic has the appropriate access policy, and that the subscriber list contains only those users who are allowed to see information about the resource. If the Amazon SNS topic is a topic that was used for CodeCommit notifications before November 5, 2019, it will contain a policy that allows CodeCommit to publish to it that contains different permissions than those required for AWS CodeStar Notifications. Using these topics is not recommended. If you want to use one created for that experience, you must add the required policy for AWS CodeStar Notifications in addition to the one that already exists. For more information, see [Configure Amazon SNS topics for notifications](set-up-sns.md) and [Understanding notification contents and security](security.md#security-notifications). 

If you want to extend the reach of notifications, you can manually configure integration between notifications and AWS Chatbot so that notifications are sent to Amazon Chime chatrooms. For more information, see [Targets](concepts.md#targets) and [To integrate notifications with AWS Chatbot and Amazon Chime](notifications-chatbot.md#notifications-chatbot-chime).<a name="notification-target-create-console"></a>

# To configure an existing Amazon SNS topic to use as a notification rule target (console)


1. Sign in to the AWS Management Console and open the Amazon SNS console at [https://console.aws.amazon.com/sns/v3/home](https://console.aws.amazon.com/sns/v3/home).

1. In the navigation bar, choose **Topics**. Choose the topic, and then choose **Edit**.

1. Expand **Access policy**, and then choose **Advanced**.

1. In the JSON editor, add the following statement to the policy. Include the topic ARN, AWS Region, AWS account ID, and topic name.

   ```
   	{
         "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"
       }
   ```

   The policy statement should look like the following.

   ```
   {
     "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. Choose **Save changes**.

1. In **Subscriptions**, review the list of topic subscribers. Add, edit, or delete subscribers as appropriate for this notification rule target. Make sure that the subscriber list contains only those users who are allowed to see information about the resource. For more information, see [Understanding notification contents and security](security.md#security-notifications).

# To create an AWS Chatbot client with Slack to use as a target


1. Follow the instructions in [Setting up AWS Chatbot with Slack](https://docs.aws.amazon.com/chatbot/latest/adminguide/slack-setup.html#slack-client-setup) in the *AWS Chatbot Administrator Guide*. When you do so, consider the following choices for optimal integration with notifications:
   + When creating an IAM role, consider choosing a role name that makes it easy to identify the purpose of this role (for example, **AWSCodeStarNotifications-Chatbot-Slack-Role**). This can help you identify the purpose of the role in the future.
   + In **SNS topics**, you don't have to choose a topic or an AWS Region. When you choose the AWS Chatbot client as a [target](concepts.md#targets), an Amazon SNS topic with all the required permissions is created and configured for the AWS Chatbot client as part of the notification rule creation process.

1. Complete the client creation process. This client is then available for you to choose as a target when creating notification rules. For more information, see [Create a notification rule](notification-rule-create.md).
**Note**  
Do not remove the Amazon SNS topic from the AWS Chatbot client after it has been configured for you. Doing so will prevent notifications from being sent to Slack.

# To create an AWS Chatbot client with Microsoft Teams to use as a target


1. Follow the instructions in [Setting up AWS Chatbot with Microsoft Teams](https://docs.aws.amazon.com/chatbot/latest/adminguide/teams-setup.html#teams-client-setup) in the *AWS Chatbot Administrator Guide*. When you do so, consider the following choices for optimal integration with notifications:
   + When creating an IAM role, consider choosing a role name that makes it easy to identify the purpose of this role (for example, **AWSCodeStarNotifications-Chatbot-Microsoft-Teams-Role**). This can help you identify the purpose of the role in the future.
   + In **SNS topics**, you don't have to choose a topic or an AWS Region. When you choose the AWS Chatbot client as a [target](concepts.md#targets), an Amazon SNS topic with all the required permissions is created and configured for the AWS Chatbot client as part of the notification rule creation process.

1. Complete the client creation process. This client is then available for you to choose as a target when creating notification rules. For more information, see [Create a notification rule](notification-rule-create.md).
**Note**  
Do not remove the Amazon SNS topic from the AWS Chatbot client after it has been configured for you. Doing so will prevent notifications from being sent to Microsoft Teams.

# View notification rule targets


You can use Developer Tools console, not the Amazon SNS console to view all of the notification rule targets for all resources in an AWS Region. You can also view the details of a notification rule target. <a name="notification-target-view-console"></a>

# To view notification rule targets (console)


1. Open the AWS Developer Tools console at [https://console.aws.amazon.com/codesuite/settings/notifications](https://console.aws.amazon.com/codesuite/settings/notifications/).

1. In the navigation bar, expand **Settings**, and then choose **Notification rules**.

1. In **Notification rule targets**, review the list of targets used by notification rules in in your AWS account in the AWS Region where you are currently signed in. Use the selector to change the AWS Region. If the target status shows as **Unreachable**, you might need to investigate. For more information, see [Troubleshooting](troubleshooting.md).<a name="notification-target-view-list-cli"></a>

# To view a list of notification rule targets (AWS CLI)


1. At a terminal or command prompt, run the **list-targets** command to view a list of all notification rule targets for the specified AWS Region:

   ```
   aws codestar-notifications list-targets --region us-east-2
   ```

1. If successful, this command returns the ID and ARN for each notification rule in the AWS Region, similar to the following:

   ```
   {
       "Targets": [
           {
               "TargetAddress": "arn:aws:sns:us-east-2:123456789012:MySNSTopicForNotificationRules",
               "TargetType": "SNS",
               "TargetStatus": "ACTIVE"
           },
           {
               "TargetAddress": "arn:aws:chatbot::123456789012:chat-configuration/slack-channel/MySlackChannelClientForMyDevTeam",
               "TargetStatus": "ACTIVE", 
               "TargetType": "AWSChatbotSlack"
           },
           {
               "TargetAddress": "arn:aws:sns:us-east-2:123456789012:MySNSTopicForNotificationsAboutMyDemoRepo",
               "TargetType": "SNS",
               "TargetStatus": "ACTIVE"
           }
       ]
   }
   ```

# Add or remove a target for a notification rule


You can edit a notification rule to change the target or targets to which it sends notifications. You can use the Developer Tools console or or the AWS CLI to change a notification rule's targets.<a name="notification-target-change-rule-console"></a>

# To change the targets for a notification rule (console)


1. Open the AWS Developer Tools console at [https://console.aws.amazon.com/codesuite/settings/notifications](https://console.aws.amazon.com/codesuite/settings/notifications/).

1. In the navigation bar, expand **Settings**, and then choose **Notification rules**.

1. In **Notification rules**, review the list of rules configured for your resources in your AWS account in the AWS Region where you are currently signed in. Use the selector to change the AWS Region.

1. Choose the rule, and then choose **Edit**. 

1. In **Targets**, do one of the following:
   + To add another target, choose **Add Target**, and then choose the Amazon SNS topic or AWS Chatbot (Slack) or AWS Chatbot (Microsoft Teams) client that you want to add from the list. You can also choose **Create SNS topic** to create a topic and add it as a target. A notification rule can have up to 10 targets.
   + To remove a target, next to the target you want to remove, choose **Remove target**.

1. Choose **Submit**.<a name="notification-target-change-rule-add-cli"></a>

# To add a target to a notification rule (AWS CLI)


1. At a terminal or command prompt, run the **subscribe** command to add a target. For example, the following command adds an Amazon SNS topic as a target for a notification rule.

   ```
   aws codestar-notifications subscribe --arn arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE --target TargetType=SNS,TargetAddress=arn:aws:sns:us-east-1:123456789012:MyNotificationTopic
   ```

1. If successful, the command returns the ARN of the updated notification rule, similar to the following.

   ```
   {
       "Arn": "arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE"
   }
   ```<a name="notification-target-change-rule-remove-cli"></a>

# To remove a target from a notification rule (AWS CLI)


1. At a terminal or command prompt, run the the **unsubscribe** command to remove a target. For example, the following command removes an Amazon SNS topic as a target for a notification rule.

   ```
   aws codestar-notifications unsubscribe --arn arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE --target TargetType=SNS,TargetAddress=arn:aws:sns:us-east-1:123456789012:MyNotificationTopic
   ```

1. If successful, the command returns the ARN of the updated notification rule and information about the removed target, similar to the following.

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

## See also

+ [Edit a notification rule](notification-rule-edit.md)
+ [Enable or disable notifications for a notification rule](notification-rule-enable-disable.md)

# Delete a notification rule target


You can delete a target if it is no longer needed. A resource can only have 10 notification rule targets configured for it, so deleting unneeded targets can help create room for other targets you might want to add to that notification rule. 

**Note**  
Deleting a notification rule target removes the target from all notification rules configured to use it as a target, but it does not delete the target itself.<a name="notification-target-delete-console"></a>

# To delete a notification rule target (console)


1. Open the AWS Developer Tools console at [https://console.aws.amazon.com/codesuite/settings/notifications](https://console.aws.amazon.com/codesuite/settings/notifications/).

1. In the navigation bar, expand **Settings**, and then choose **Notification rules**.

1. In **Notification rule targets**, review the list of targets configured for your resources in your AWS account in the AWS Region where you are currently signed in. Use the selector to change the AWS Region.

1. Choose the notification rule target, and then choose **Delete**.

1. Type **delete**, and then choose **Delete**.<a name="notification-target-delete-cli"></a>

# To delete a notification rule target (AWS CLI)


1. At a terminal or command prompt, run the **delete-target** command, specifying the ARN of the target. For example, the following command deletes a target that uses an Amazon SNS topic.

   ```
   aws codestar-notifications delete-target --target-address arn:aws:sns:us-east-1:123456789012:MyNotificationTopic
   ```

1. If successful, the command returns nothing. If unsuccessful, the command returns an error. The most common error is that the topic is the target for one or more notification rules. 

   ```
   An error occurred (ValidationException) when calling the DeleteTarget operation: Unsubscribe target before deleting.
   ```

   You can use the `--force-unsubscribe-all` parameter to remove the target from all notification rules configured to use it as a target, and then delete the target.

   ```
   aws codestar-notifications delete-target --target-address arn:aws:sns:us-east-1:123456789012:MyNotificationTopic --force-unsubscribe-all
   ```