

文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 [AWS SDK 範例](https://github.com/awsdocs/aws-doc-sdk-examples)。

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

# AWS CodeStar 使用 的通知範例 AWS CLI
<a name="cli_2_codestar-notifications_code_examples"></a>

下列程式碼範例示範如何使用 AWS Command Line Interface 搭配 AWS CodeStar 通知來執行動作和實作常見案例。

*Actions* 是大型程式的程式碼摘錄，必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數，但您可以在其相關情境中查看內容中的動作。

每個範例均包含完整原始碼的連結，您可在連結中找到如何設定和執行內容中程式碼的相關指示。

**Topics**
+ [動作](#actions)

## 動作
<a name="actions"></a>

### `create-notification-rule`
<a name="codestar-notifications_CreateNotificationRule_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `create-notification-rule`。

**AWS CLI**  
**建立通知規則**  
下列`create-notification-rule`範例使用名為 的 JSON 檔案`rule.json`，`MyNotificationRule`為指定 AWS 帳戶中名為 `MyDemoRepo`的儲存庫建立名為 的通知規則。建立分支和標籤時，具有 `FULL` 詳細資訊類型的通知會傳送至指定的目標 Amazon SNS 主題。  

```
aws codestar-notifications create-notification-rule \
    --cli-input-json file://rule.json
```
`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"
}
```
輸出：  

```
{
    "Arn": "arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE"
}
```
如需詳細資訊，請參閱《*AWS 開發人員工具主控台使用者指南*》中的[建立通知規則](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/notification-rule-create.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [CreateNotificationRule](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-notifications/create-notification-rule.html)。

### `delete-notification-rule`
<a name="codestar-notifications_DeleteNotificationRule_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `delete-notification-rule`。

**AWS CLI**  
**刪除通知規則**  
以下 `delete-notification-rule` 範例會刪除指定的通知規則。  

```
aws codestar-notifications delete-notification-rule \
    --arn arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE
```
輸出：  

```
{
    "Arn": "arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE"
}
```
如需詳細資訊，請參閱《*AWS 開發人員工具主控台使用者指南*》中的[刪除通知規則](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/notification-rule-delete.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DeleteNotificationRule](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-notifications/delete-notification-rule.html)。

### `delete-target`
<a name="codestar-notifications_DeleteTarget_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `delete-target`。

**AWS CLI**  
**刪除通知規則目標**  
下列 `delete-target` 範例將指定的目標從設定為當作目標使用的所有通知規則中移除，然後刪除該目標。  

```
aws codestar-notifications  delete-target \
    --target-address arn:aws:sns:us-east-1:123456789012:MyNotificationTopic \
    --force-unsubscribe-all
```
此命令不會產生輸出。  
如需詳細資訊，請參閱《*AWS 開發人員工具主控台使用者指南*》中的[刪除通知規則目標](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/notification-target-delete.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DeleteTarget](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-notifications/delete-target.html)。

### `describe-notification-rule`
<a name="codestar-notifications_DescribeNotificationRule_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `describe-notification-rule`。

**AWS CLI**  
**擷取通知規則的詳細資訊**  
下列 `describe-notification-rule` 範例會擷取指定之通知規則的詳細資訊。  

```
aws codestar-notifications describe-notification-rule \
    --arn arn:aws:codestar-notifications:us-west-2:123456789012:notificationrule/dc82df7a-EXAMPLE
```
輸出：  

```
{
    "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"
}
```
如需詳細資訊，請參閱《*AWS 開發人員工具主控台使用者指南*》中的[檢視通知規則](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/notification-rule-view.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DescribeNotificationRule](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-notifications/describe-notification-rule.html)。

### `list-event-types`
<a name="codestar-notifications_ListEventTypes_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `list-event-types`。

**AWS CLI**  
**取得通知規則的事件類型清單**  
下列 `list-event-types` 範例擷取 CodeDeploy 應用程式之所有可用通知事件類型的篩選清單。如果您未使用篩選條件，該命令會傳回所有資源類型的所有通知事件類型。  

```
aws codestar-notifications list-event-types \
    --filters Name=SERVICE_NAME,Value=CodeDeploy
```
輸出：  

```
{
    "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"
        }
    ]
}
```
如需詳細資訊，請參閱《*AWS 開發人員工具主控台使用者指南*》中的[建立通知規則](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/notification-rule-create.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [ListEventTypes](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-notifications/list-event-types.html)。

### `list-notification-rules`
<a name="codestar-notifications_ListNotificationRules_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `list-notification-rules`。

**AWS CLI**  
**擷取通知規則清單**  
下列`list-notification-rules`範例會擷取指定區域中所有通知規則的清單 AWS 。  

```
aws codestar-notifications list-notification-rules --region us-east-1
```
輸出：  

```
{
    "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"
        }
    ]
}
```
如需詳細資訊，請參閱《*AWS 開發人員工具主控台使用者指南*》中的[檢視通知規則](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/notification-rule-view.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [ListNotificationRules](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-notifications/list-notification-rules.html)。

### `list-tags-for-resource`
<a name="codestar-notifications_ListTagsForResource_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `list-tags-for-resource`。

**AWS CLI**  
**取得附加於通知規則的標籤清單**  
下列 `list-tags-for-resource` 範例擷取附加於指定之通知規則的所有標籤清單。在此範例中，通知規則目前沒有關聯的標籤。  

```
aws codestar-notifications list-tags-for-resource \
    --arn arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/fe1efd35-EXAMPLE
```
輸出：  

```
{
    "Tags": {}
}
```
如需詳細資訊，請參閱《*AWS 開發人員工具主控台使用者指南*》中的[建立通知規則](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/notification-rule-create.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [ListTagsForResource](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-notifications/list-tags-for-resource.html)。

### `list-targets`
<a name="codestar-notifications_ListTargets_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `list-targets`。

**AWS CLI**  
**擷取通知規則目標的清單**  
下列`list-targets`範例會擷取指定區域中所有通知規則目標的清單 AWS 。  

```
aws codestar-notifications list-targets \
    --region us-east-1
```
輸出：  

```
{
    "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"
        }
    ]
}
```
如需詳細資訊，請參閱《*AWS 開發人員工具主控台使用者指南*》中的[檢視通知規則目標](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/notification-target-view.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [ListTargets](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-notifications/list-targets.html)。

### `subscribe`
<a name="codestar-notifications_Subscribe_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `subscribe`。

**AWS CLI**  
**新增目標至通知規則**  
下列 `subscribe` 範例新增 Amazon SNS 主題做為指定之通知規則的目標。  

```
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
```
輸出：  

```
{
    "Arn": "arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE"
}
```
如需詳細資訊，請參閱《*AWS 開發人員工具主控台使用者指南*》中的[新增或移除 Amazon SNS 主題做為通知規則的目標](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/notification-target-change-rule.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [Subscribe](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-notifications/subscribe.html)。

### `tag-resource`
<a name="codestar-notifications_TagResource_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `tag-resource`。

**AWS CLI**  
**新增標籤至通知規則**  
下列 `tag-resource` 範例將索引鍵名稱 `Team` 和值 `Li_Juan` 的標籤，新增至指定的通知規則。  

```
aws codestar-notifications tag-resource \
    --arn arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/fe1efd35-EXAMPLE \
    --tags Team=Li_Juan
```
輸出：  

```
{
    "Tags": {
        "Team": "Li_Juan"
    }
}
```
如需詳細資訊，請參閱《*AWS 開發人員工具主控台使用者指南*》中的[建立通知規則](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/notification-rule-create.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [TagResource](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-notifications/tag-resource.html)。

### `unsubscribe`
<a name="codestar-notifications_Unsubscribe_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `unsubscribe`。

**AWS CLI**  
**從通知規則移除目標**  
下列 `unsubscribe` 範例將做為目標的 Amazon SNS 主題，從指定的通知規則中移除。  

```
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
```
輸出：  

```
{
    "Arn": "arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE"
    "TargetAddress": "arn:aws:sns:us-east-1:123456789012:MyNotificationTopic"
}
```
如需詳細資訊，請參閱《*AWS 開發人員工具主控台使用者指南*》中的[新增或移除 Amazon SNS 主題做為通知規則的目標](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/notification-target-change-rule.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [Unsubscribe](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-notifications/unsubscribe.html)。

### `untag-resource`
<a name="codestar-notifications_UntagResource_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `untag-resource`。

**AWS CLI**  
**從通知規則移除標籤**  
下列 `untag-resource` 範例從指定的通知規則中移除具有索引鍵名稱 `Team` 的標籤。  

```
aws codestar-notifications untag-resource \
    --arn arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/fe1efd35-EXAMPLE \
    --tag-keys Team
```
此命令不會產生輸出。  
如需詳細資訊，請參閱《*AWS 開發人員工具主控台使用者指南*》中的[編輯通知規則](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/notification-rule-edit.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [UntagResource](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-notifications/untag-resource.html)。

### `update-notification-rule`
<a name="codestar-notifications_UpdateNotificationRule_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `update-notification-rule`。

**AWS CLI**  
**更新通知規則**  
下列`update-notification-rule`範例`123456789012`會使用名為 `MyNotificationRule`的 JSON 檔案，更新 AWS 帳戶中名為 的通知規則`update.json`。  

```
aws codestar-notifications update-notification-rule \
    --cli-input-json file://update.json
```
`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"
}
```
輸出：  

```
{
    "Arn": "arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE"
}
```
如需詳細資訊，請參閱《*AWS 開發人員工具主控台使用者指南*》中的[編輯通知規則](https://docs.aws.amazon.com/dtconsole/latest/userguide/notification-rule-edit.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [UpdateNotificationRule](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-notifications/update-notification-rule.html)。