

# AWS CLI を使用した AWS CodeStar Notifications の例
<a name="cli_codestar-notifications_code_examples"></a>

次のコード例は、AWS CodeStar Notifications で AWS Command Line Interfaceを使用してアクションを実行し、一般的なシナリオを実装する方法を示しています。

*アクション*はより大きなプログラムからのコードの抜粋であり、コンテキスト内で実行する必要があります。アクションは個々のサービス機能を呼び出す方法を示していますが、コンテキスト内のアクションは、関連するシナリオで確認できます。

各例には完全なソースコードへのリンクが含まれており、コードの設定方法と実行方法に関する手順を確認できます。

**Topics**
+ [アクション](#actions)

## アクション
<a name="actions"></a>

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

次のコード例は、`create-notification-rule` を使用する方法を示しています。

**AWS CLI**  
**通知ルールを作成するには**  
次の `create-notification-rule` の例では、`rule.json` という名前の JSON ファイルを使用して、指定された AWS アカウントで `MyDemoRepo` という名前のリポジトリに `MyNotificationRule` という名前の通知ルールを作成しています。ブランチとタグが作成されると、詳細タイプが `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 Developer Tools Console ユーザーガイド*」の「[Create a Notification rule](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_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 Developer Tools Console ユーザーガイド*」の「[Delete a Notification Rule](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_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 Developer Tools Console ユーザーガイド*」の「[Delete a Notification Rule Target](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_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 Developer Tools Console ユーザーガイド*」の「[View Notification Rules](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_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 Developer Tools Console ユーザーガイド*」の「[Create a Notification Rule](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_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 Developer Tools Console ユーザーガイド*」の「[View Notification Rules](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_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 Developer Tools Console ユーザーガイド*」の「[Create a Notification Rule](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_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 Developer Tools Console ユーザーガイド*」の「[View Notification Rule Targets](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_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"
}
```
詳細については、「*AWSDeveloper Tools Console ユーザーガイド*」の「[Add or Remove an Amazon SNS Topic as a Target for a Notification Rule](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_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 Developer Tools Console ユーザーガイド*」の「[Create a Notification Rule](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_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"
}
```
詳細については、「*AWSDeveloper Tools Console ユーザーガイド*」の「[Add or Remove an Amazon SNS Topic as a Target for a Notification Rule](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_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 Developer Tools console ユーザーガイド*」の「[Edit a Notification Rule](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_topic"></a>

次のコード例は、`update-notification-rule` を使用する方法を示しています。

**AWS CLI**  
**通知ルールを更新するには**  
次の `update-notification-rule` の例では、`update.json` という名前の JSON ファイルを使用して、AWS アカウント `123456789012` で `MyNotificationRule` という名前の通知ルールを更新しています。  

```
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 Developer Tools console ユーザーガイド*」の「[Edit a notification rule](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)」を参照してください。