AWS CodeStar Notifications examples using Amazon CLI
The following code examples show you how to perform actions and implement common scenarios by using the Amazon Command Line Interface with AWS CodeStar Notifications.
Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.
Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.
Topics
Actions
The following code example shows how to use create-notification-rule.
- Amazon CLI
-
To create a notification rule
The following
create-notification-ruleexample uses a JSON file namedrule.jsonto create a notification rule namedMyNotificationRulefor a repository namedMyDemoRepoin the specified Amazon account. Notifications with theFULLdetail type are sent to the specified target Amazon SNS topic when branches and tags are created.aws codestar-notifications create-notification-rule \ --cli-input-jsonfile://rule.jsonContents of
rule.json:{ "Name": "MyNotificationRule", "EventTypeIds": [ "codecommit-repository-branches-and-tags-created" ], "Resource": "arn:aws:codecommit:us-east-1:123456789012:MyDemoRepo", "Targets": [ { "TargetType": "SNS", "TargetAddress": "arn:aws:sns:us-east-1:123456789012:MyNotificationTopic" } ], "Status": "ENABLED", "DetailType": "FULL" }Output:
{ "Arn": "arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE" }For more information, see Create a Notification rule
in the Amazon Developer Tools Console User Guide. -
For API details, see CreateNotificationRule
in Amazon CLI Command Reference.
-
The following code example shows how to use delete-notification-rule.
- Amazon CLI
-
To delete a notification rule
The following
delete-notification-ruleexample deletes the specified notification rule.aws codestar-notifications delete-notification-rule \ --arnarn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLEOutput:
{ "Arn": "arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE" }For more information, see Delete a Notification Rule
in the Amazon Developer Tools Console User Guide. -
For API details, see DeleteNotificationRule
in Amazon CLI Command Reference.
-
The following code example shows how to use delete-target.
- Amazon CLI
-
To delete a notification rule target
The following
delete-targetexample removes the specified target from all notification rules configured to use it as a target, and then deletes the target.aws codestar-notifications delete-target \ --target-addressarn:aws:sns:us-east-1:123456789012:MyNotificationTopic\ --force-unsubscribe-allThis command produces no output.
For more information, see Delete a Notification Rule Target
in the Amazon Developer Tools Console User Guide. -
For API details, see DeleteTarget
in Amazon CLI Command Reference.
-
The following code example shows how to use describe-notification-rule.
- Amazon CLI
-
To retrieve details of a notification rule
The following
describe-notification-ruleexample retrieves the details of the specified notification rule.aws codestar-notifications describe-notification-rule \ --arnarn:aws:codestar-notifications:us-west-2:123456789012:notificationrule/dc82df7a-EXAMPLEOutput:
{ "LastModifiedTimestamp": 1569199844.857, "EventTypes": [ { "ServiceName": "CodeCommit", "EventTypeName": "Branches and tags: Created", "ResourceType": "Repository", "EventTypeId": "codecommit-repository-branches-and-tags-created" } ], "Status": "ENABLED", "DetailType": "FULL", "Resource": "arn:aws:codecommit:us-west-2:123456789012:MyDemoRepo", "Arn": "arn:aws:codestar-notifications:us-west-w:123456789012:notificationrule/dc82df7a-EXAMPLE", "Targets": [ { "TargetStatus": "ACTIVE", "TargetAddress": "arn:aws:sns:us-west-2:123456789012:MyNotificationTopic", "TargetType": "SNS" } ], "Name": "MyNotificationRule", "CreatedTimestamp": 1569199844.857, "CreatedBy": "arn:aws:iam::123456789012:user/Mary_Major" }For more information, see View Notification Rules
in the Amazon Developer Tools Console User Guide. -
For API details, see DescribeNotificationRule
in Amazon CLI Command Reference.
-
The following code example shows how to use list-event-types.
- Amazon CLI
-
To get a list of event types for a notification rule
The following
list-event-typesexample retrieves a filtered list of all available notification event types for CodeDeploy applications. If instead you use no filter, the command returns all notification event types for all resource types.aws codestar-notifications list-event-types \ --filtersName=SERVICE_NAME,Value=CodeDeployOutput:
{ "EventTypes": [ { "EventTypeId": "codedeploy-application-deployment-succeeded", "ServiceName": "CodeDeploy", "EventTypeName": "Deployment: Succeeded", "ResourceType": "Application" }, { "EventTypeId": "codedeploy-application-deployment-failed", "ServiceName": "CodeDeploy", "EventTypeName": "Deployment: Failed", "ResourceType": "Application" }, { "EventTypeId": "codedeploy-application-deployment-started", "ServiceName": "CodeDeploy", "EventTypeName": "Deployment: Started", "ResourceType": "Application" } ] }For more information, see Create a Notification Rule
in the Amazon Developer Tools Console User Guide. -
For API details, see ListEventTypes
in Amazon CLI Command Reference.
-
The following code example shows how to use list-notification-rules.
- Amazon CLI
-
To retrieve a list of notification rules
The following
list-notification-rulesexample retrieves a list of all notification rules in the specified Amazon Region.aws codestar-notifications list-notification-rules --regionus-east-1Output:
{ "NotificationRules": [ { "Id": "dc82df7a-EXAMPLE", "Arn": "arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE" }, { "Id": "8d1f0983-EXAMPLE", "Arn": "arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/8d1f0983-EXAMPLE" } ] }For more information, see View Notification Rules
in the Amazon Developer Tools Console User Guide. -
For API details, see ListNotificationRules
in Amazon CLI Command Reference.
-
The following code example shows how to use list-tags-for-resource.
- Amazon CLI
-
To get a list of tags attached to a notification rule
The following
list-tags-for-resourceexample retrieves a list of all tags attached to the specified notification rule. In this example, the notification rule currently has no tags associated with it.aws codestar-notifications list-tags-for-resource \ --arnarn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/fe1efd35-EXAMPLEOutput:
{ "Tags": {} }For more information, see Create a Notification Rule
in the Amazon Developer Tools Console User Guide. -
For API details, see ListTagsForResource
in Amazon CLI Command Reference.
-
The following code example shows how to use list-targets.
- Amazon CLI
-
To retrieve a list of notification rule targets
The following
list-targetsexample retrieves a list of all notification rule targets in the specified Amazon Region.aws codestar-notifications list-targets \ --regionus-east-1Output:
{ "Targets": [ { "TargetAddress": "arn:aws:sns:us-east-1:123456789012:MySNSTopicForNotificationRules", "TargetType": "SNS", "TargetStatus": "ACTIVE" }, { "TargetAddress": "arn:aws:sns:us-east-1:123456789012:MySNSTopicForNotificationsAboutMyDemoRepo", "TargetType": "SNS", "TargetStatus": "ACTIVE" } ] }For more information, see View Notification Rule Targets
in the Amazon Developer Tools Console User Guide. -
For API details, see ListTargets
in Amazon CLI Command Reference.
-
The following code example shows how to use subscribe.
- Amazon CLI
-
To add a target to a notification rule
The following
subscribeexample adds an Amazon SNS topic as a target for the specified notification rule.aws codestar-notifications subscribe \ --arnarn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE\ --targetTargetType=SNS,TargetAddress=arn:aws:sns:us-east-1:123456789012:MyNotificationTopicOutput:
{ "Arn": "arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE" }For more information, see Add or Remove an Amazon SNS Topic as a Target for a Notification Rule
in the Amazon Developer Tools Console User Guide. -
For API details, see Subscribe
in Amazon CLI Command Reference.
-
The following code example shows how to use tag-resource.
- Amazon CLI
-
To add a tag to a notification rule
The following
tag-resourceexample adds a tag with the key name ofTeamand the value ofLi_Juanto the specified notification rule.aws codestar-notifications tag-resource \ --arnarn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/fe1efd35-EXAMPLE\ --tagsTeam=Li_JuanOutput:
{ "Tags": { "Team": "Li_Juan" } }For more information, see Create a Notification Rule
in the Amazon Developer Tools Console User Guide. -
For API details, see TagResource
in Amazon CLI Command Reference.
-
The following code example shows how to use unsubscribe.
- Amazon CLI
-
To remove a target from a notification rule
The following
unsubscribeexample removes an Amazon SNS topic as a target from the specified notification rule.aws codestar-notifications unsubscribe \ --arnarn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE\ --targetTargetType=SNS,TargetAddress=arn:aws:sns:us-east-1:123456789012:MyNotificationTopicOutput:
{ "Arn": "arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE" "TargetAddress": "arn:aws:sns:us-east-1:123456789012:MyNotificationTopic" }For more information, see Add or Remove an Amazon SNS Topic as a Target for a Notification Rule
in the Amazon Developer Tools Console User Guide. -
For API details, see Unsubscribe
in Amazon CLI Command Reference.
-
The following code example shows how to use untag-resource.
- Amazon CLI
-
To remove a tag from a notification rule
The following
untag-resourceexample removes the tag with the key nameTeamfrom the specified notification rule.aws codestar-notifications untag-resource \ --arnarn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/fe1efd35-EXAMPLE\ --tag-keysTeamThis command produces no output.
For more information, see Edit a Notification Rule
in the Amazon Developer Tools Console User Guide. -
For API details, see UntagResource
in Amazon CLI Command Reference.
-
The following code example shows how to use update-notification-rule.
- Amazon CLI
-
To update a notification rule
The following
update-notification-ruleexample updates a notification rule namedMyNotificationRulein the Amazon account123456789012using a JSON file namedupdate.json.aws codestar-notifications update-notification-rule \ --cli-input-jsonfile://update.jsonContents of
update.json:{ "Name": "MyUpdatedNotificationRule", "EventTypeIds": [ "codecommit-repository-branches-and-tags-created" ], "Resource": "arn:aws:codecommit:us-east-1:123456789012:MyDemoRepo", "Targets": [ { "TargetType": "SNS", "TargetAddress": "arn:aws:sns:us-east-1:123456789012:MyNotificationTopic" } ], "Status": "ENABLED", "DetailType": "FULL" }Output:
{ "Arn": "arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE" }For more information, see Edit a notification rule
in the Amazon Developer Tools Console User Guide. -
For API details, see UpdateNotificationRule
in Amazon CLI Command Reference.
-