

# AWS CLI を使用した Detective の例
<a name="cli_detective_code_examples"></a>

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

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

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

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

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

### `accept-invitation`
<a name="detective_AcceptInvitation_cli_topic"></a>

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

**AWS CLI**  
**動作グラフのメンバーアカウントになるための招待を受け入れるには**  
次の `accept-invitation` の例では、動作グラフ arn:aws:detective:us-east-1:111122223333:graph:123412341234 のメンバーアカウントになるための招待を受け入れます。  

```
aws detective accept-invitation \
    --graph-arn arn:aws:detective:us-east-1:111122223333:graph:123412341234
```
このコマンドでは何も出力されません。  
詳細については、「*Amazon Detective Administration Guide*」の[「Responding to a behavior graph invitation](https://docs.aws.amazon.com/detective/latest/adminguide/member-invitation-response.html)」を参照してください。  
+  API の詳細については、「*AWS CLI コマンドリファレンス*」の「[AcceptInvitation](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/detective/accept-invitation.html)」を参照してください。

### `create-graph`
<a name="detective_CreateGraph_cli_topic"></a>

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

**AWS CLI**  
**Amazon Detective を有効にして新しい動作グラフを作成するには**  
次の `create-graph` の例では、コマンドが実行されているリージョンでコマンドを実行する AWS アカウントに対して Detective を有効にします。そのアカウントを管理者アカウントとする新しい動作グラフが作成されます。このコマンドは、部門タグに「財務」という値を割り当てます。  

```
aws detective create-graph \
    --tags '{"Department": "Finance"}'
```
出力:  

```
{
    "GraphArn": "arn:aws:detective:us-east-1:111122223333:graph:027c7c4610ea4aacaf0b883093cab899"
}
```
詳細については、「*Amazon Detective Administration Guide*」の「[Enabling Amazon Detective](https://docs.aws.amazon.com/detective/latest/adminguide/detective-enabling.html)」を参照してください。  
+  API の詳細については、「*AWS CLI コマンドリファレンス*」の「[CreateGraph](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/detective/create-graph.html)」を参照してください。

### `create-members`
<a name="detective_CreateMembers_cli_topic"></a>

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

**AWS CLI**  
**動作グラフにメンバーアカウントを招待するには**  
次の `create-members` の例では、動作グラフ arn:aws:detective:us-east-1:111122223333:graph:123412341234 のメンバーアカウントになるよう 2 つの AWS アカウントを招待します。アカウントごとに、このリクエストで AWS アカウント ID とアカウントルートユーザーのメールアドレスが提供されます。リクエストには、招待メールに挿入するカスタムメッセージが含まれています。  

```
aws detective create-members \
    --accounts AccountId=444455556666,EmailAddress=mmajor@example.com AccountId=123456789012,EmailAddress=jstiles@example.com \
    --graph-arn arn:aws:detective:us-east-1:111122223333:graph:123412341234 \
    --message "This is Paul Santos. I need to add your account to the data we use for security investigation in Amazon Detective. If you have any questions, contact me at psantos@example.com."
```
出力:  

```
{
    "Members": [
    {
        "AccountId": "444455556666",
        "AdministratorId": "111122223333",
        "EmailAddress": "mmajor@example.com",
        "GraphArn": "arn:aws:detective:us-east-1:111122223333:graph:123412341234",
        "InvitedTime": 1579826107000,
        "MasterId": "111122223333",
        "Status": "INVITED",
        "UpdatedTime": 1579826107000
   },
   {
        "AccountId": "123456789012",
        "AdministratorId": "111122223333",
        "EmailAddress": "jstiles@example.com",
        "GraphArn": "arn:aws:detective:us-east-1:111122223333:graph:123412341234",
        "InvitedTime": 1579826107000,
        "MasterId": "111122223333",
        "Status": "VERIFICATION_IN_PROGRESS",
        "UpdatedTime": 1579826107000
     }
    ],
    "UnprocessedAccounts": [ ]
}
```
詳細については、「*Amazon Detective Administration Guide*」の「Inviting member accounts to a behavior graph<https://docs.aws.amazon.com/detective/latest/adminguide/graph-admin-add-member-accounts.html>」を参照してください。  
**招待メールを送信せずにメンバーアカウントを招待するには**  
次の `create-members` の例では、動作グラフ arn:aws:detective:us-east-1:111122223333:graph:123412341234 のメンバーアカウントになるよう 2 つの AWS アカウントを招待します。アカウントごとに、このリクエストで AWS アカウント ID とアカウントルートユーザーのメールアドレスが提供されます。メンバーアカウントには招待メールは届きません。  

```
aws detective create-members \
    --accounts AccountId=444455556666,EmailAddress=mmajor@example.com AccountId=123456789012,EmailAddress=jstiles@example.com \
    --graph-arn arn:aws:detective:us-east-1:111122223333:graph:123412341234 \
    --disable-email-notification
```
出力:  

```
{
    "Members": [
    {
        "AccountId": "444455556666",
        "AdministratorId": "111122223333",
        "EmailAddress": "mmajor@example.com",
        "GraphArn": "arn:aws:detective:us-east-1:111122223333:graph:123412341234",
        "InvitedTime": 1579826107000,
        "MasterId": "111122223333",
        "Status": "INVITED",
        "UpdatedTime": 1579826107000
   },
   {
        "AccountId": "123456789012",
        "AdministratorId": "111122223333",
        "EmailAddress": "jstiles@example.com",
        "GraphArn": "arn:aws:detective:us-east-1:111122223333:graph:123412341234",
        "InvitedTime": 1579826107000,
        "MasterId": "111122223333",
        "Status": "VERIFICATION_IN_PROGRESS",
        "UpdatedTime": 1579826107000
     }
    ],
    "UnprocessedAccounts": [ ]
}
```
詳細については、「*Amazon Detective Administration Guide*」の「Inviting member accounts to a behavior graph<https://docs.aws.amazon.com/detective/latest/adminguide/graph-admin-add-member-accounts.html>」を参照してください。  
+  API の詳細については、「*AWS CLI コマンドリファレンス*」の「[CreateMembers](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/detective/create-members.html)」を参照してください。

### `delete-graph`
<a name="detective_DeleteGraph_cli_topic"></a>

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

**AWS CLI**  
**Detective を無効にして動作グラフを削除するには**  
次の `delete-graph` の例では、Detective を無効にし、指定された動作グラフを削除します。  

```
aws detective delete-graph \
    --graph-arn arn:aws:detective:us-east-1:111122223333:graph:123412341234
```
このコマンドでは何も出力されません。  
詳細については、「*Amazon Detective Administration Guide*」の「[Disabling Amazon Detective](https://docs.aws.amazon.com/detective/latest/adminguide/detective-disabling.html)」を参照してください。  
+  API の詳細については、「*AWS CLI コマンドリファレンス*」の「[DeleteGraph](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/detective/delete-graph.html)」を参照してください。

### `delete-members`
<a name="detective_DeleteMembers_cli_topic"></a>

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

**AWS CLI**  
**動作グラフからメンバーアカウントを削除するには**  
次の `delete-members` の例では、動作グラフ arn:aws:detective:us-east-1:111122223333:graph:123412341234 から 2 つのメンバーアカウントを削除します。アカウントを識別するには、リクエストで AWS アカウント ID を提供します。  

```
aws detective delete-members \
    --account-ids 444455556666 123456789012 \
    --graph-arn arn:aws:detective:us-east-1:111122223333:graph:123412341234
```
出力:  

```
 {
    "AccountIds": [ "444455556666", "123456789012" ],
    "UnprocessedAccounts": [ ]
}
```
詳細については、「*Amazon Detective Administration Guide*」の「Removing member accounts from a behavior graph<https://docs.aws.amazon.com/detective/latest/adminguide/graph-admin-remove-member-accounts.html>」を参照してください。  
+  API の詳細については、「*AWS CLI コマンドリファレンス*」の「[DeleteMembers](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/detective/delete-members.html)」を参照してください。

### `disassociate-membership`
<a name="detective_DisassociateMembership_cli_topic"></a>

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

**AWS CLI**  
**動作グラフのメンバーシップを辞退するには**  
次のメンバーシップの関連付け解除の例では、動作グラフ arn:aws:detective:us-east-1:111122223333:graph:123412341234 からコマンドを実行する AWS アカウントを削除します。  

```
aws detective disassociate-membership \
     --graph-arn arn:aws:detective:us-east-1:111122223333:graph:123412341234
```
詳細については、「*Amazon Detective Administration Guide*」の「Removing your account from a behavior graph<https://docs.aws.amazon.com/detective/latest/adminguide/member-remove-self-from-graph.html>」を参照してください。  
+  API の詳細については、「*AWS CLI コマンドリファレンス*」の「[DisassociateMembership](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/detective/disassociate-membership.html)」を参照してください。

### `get-members`
<a name="detective_GetMembers_cli_topic"></a>

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

**AWS CLI**  
**選択した動作グラフメンバーアカウントに関する情報を取得するには**  
次の `get-members` の例では、動作グラフ arn:aws:detective:us-east-1:111122223333:graph:123412341234 の 2 つのメンバーアカウントに関する情報を取得します。2 つのアカウントに対し、リクエストで AWS アカウント ID が提供されます。  

```
aws detective get-members \
    --account-ids 444455556666 123456789012 \
    --graph-arn arn:aws:detective:us-east-1:111122223333:graph:123412341234
```
出力:  

```
{
    "MemberDetails": [
    {
        "AccountId": "444455556666",
        "AdministratorId": "111122223333",
        "EmailAddress": "mmajor@example.com",
        "GraphArn": "arn:aws:detective:us-east-1:111122223333:graph:123412341234",
        "InvitedTime": 1579826107000,
        "MasterId": "111122223333",
        "Status": "INVITED",
        "UpdatedTime": 1579826107000
    }
    {
        "AccountId": "123456789012",
        "AdministratorId": "111122223333",
        "EmailAddress": "jstiles@example.com",
        "GraphArn": "arn:aws:detective:us-east-1:111122223333:graph:123412341234",
        "InvitedTime": 1579826107000,
        "MasterId": "111122223333",
        "Status": "INVITED",
        "UpdatedTime": 1579826107000
    }
],
    "UnprocessedAccounts": [ ]
}
```
詳細については、「*Amazon Detective Administration Guide*」の「Viewing the list of accounts in a behavior graph<https://docs.aws.amazon.com/detective/latest/adminguide/graph-admin-view-accounts.html>」を参照してください。  
+  API の詳細については、「*AWS CLI コマンドリファレンス*」の「[GetMembers](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/detective/get-members.html)」を参照してください。

### `list-graphs`
<a name="detective_ListGraphs_cli_topic"></a>

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

**AWS CLI**  
**自身のアカウントが管理者になっている動作グラフのリストを表示するには**  
次の `list-graphs` の例は、呼び出し元アカウントが現在のリージョン内で管理者となっている動作グラフを取得します。  

```
aws detective list-graphs
```
出力:  

```
{
    "GraphList": [
        {
            "Arn": "arn:aws:detective:us-east-1:111122223333:graph:123412341234",
            "CreatedTime": 1579736111000
        }
    ]
}
```
+  API の詳細については、「*AWS CLI コマンドリファレンス*」の「[ListGraphs](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/detective/list-graphs.html)」を参照してください。

### `list-invitations`
<a name="detective_ListInvitations_cli_topic"></a>

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

**AWS CLI**  
**アカウントがメンバーであるか招待されている動作グラフのリストを表示するには**  
次の `list-invitations` の例では、呼び出し元アカウントが招待されている動作グラフを取得しています。結果には、未対応の招待と承諾済みの招待のみが表示されます。拒否した招待や削除されたメンバーシップは含まれません。  

```
aws detective list-invitations
```
出力:  

```
{
    "Invitations": [
    {
        "AccountId": "444455556666",
        "AdministratorId": "111122223333",
        "EmailAddress": "mmajor@example.com",
        "GraphArn": "arn:aws:detective:us-east-1:111122223333:graph:123412341234",
        "InvitedTime": 1579826107000,
        "MasterId": "111122223333",
        "Status": "INVITED",
        "UpdatedTime": 1579826107000
    }
]
}
```
詳細については、「*Amazon Detective Administration Guide*」の「Viewing your list of behavior graph invitations<https://docs.aws.amazon.com/detective/latest/adminguide/member-view-graph-invitations.html>」を参照してください。  
+  API の詳細については、「*AWS CLI コマンドリファレンス*」の「[ListInvitations](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/detective/list-invitations.html)」を参照してください。

### `list-members`
<a name="detective_ListMembers_cli_topic"></a>

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

**AWS CLI**  
**動作グラフのメンバーアカウントを一覧表示するには**  
次の `list-members` の例では、動作グラフ `arn:aws:detective:us-east-1:111122223333:graph:123412341234` の招待された有効なメンバーアカウントを取得します。結果には、削除されたメンバーアカウントは含まれません。  

```
aws detective list-members \
    --graph-arn arn:aws:detective:us-east-1:111122223333:graph:123412341234
```
出力:  

```
{
    "MemberDetails": [
        {
            "AccountId": "444455556666",
            "AdministratorId": "111122223333",
            "EmailAddress": "mmajor@example.com",
            "GraphArn": "arn:aws:detective:us-east-1:111122223333:graph:123412341234",
            "InvitedTime": 1579826107000,
            "MasterId": "111122223333",
            "Status": "INVITED",
            "UpdatedTime": 1579826107000
        },
        {
            "AccountId": "123456789012",
            "AdministratorId": "111122223333",
            "EmailAddress": "jstiles@example.com",
            "GraphArn": "arn:aws:detective:us-east-1:111122223333:graph:123412341234",
            "InvitedTime": 1579826107000,
            "MasterId": "111122223333",
            "PercentOfGraphUtilization": 2,
            "PercentOfGraphUtilizationUpdatedTime": 1586287843,
            "Status": "ENABLED",
            "UpdatedTime": 1579973711000,
            "VolumeUsageInBytes": 200,
            "VolumeUsageUpdatedTime": 1586287843
        }
    ]
}
```
詳細については、「*Amazon Detective Administration Guide*」の「[Viewing the list of accounts in a behavior graph](https://docs.aws.amazon.com/detective/latest/adminguide/graph-admin-view-accounts.html)」を参照してください。  
+  API の詳細については、「*AWS CLI コマンドリファレンス*」の「[ListMembers](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/detective/list-members.html)」を参照してください。

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

次のコード例は、`list-tags-for-resource` を使用する方法を示しています。

**AWS CLI**  
**動作グラフに割り当てられたタグを取得するには**  
次の `list-tags-for-resource` の例では、指定された動作グラフに割り当てられたタグを返します。  

```
aws detective list-tags-for-resource \
    --resource-arn arn:aws:detective:us-east-1:111122223333:graph:123412341234
```
出力:  

```
{
    "Tags": {
        "Department" : "Finance"
    }
}
```
詳細については、「*Amazon Detective Administration Guide*」の「[Managing tags for a behavior graph](https://docs.aws.amazon.com/detective/latest/adminguide/graph-tags.html)」を参照してください。  
+  API の詳細については、「AWS CLI コマンドリファレンス」の「[ListTagsForResource](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/detective/list-tags-for-resource.html)」を参照してください。

### `reject-invitation`
<a name="detective_RejectInvitation_cli_topic"></a>

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

**AWS CLI**  
**動作グラフのメンバーアカウントになるための招待を拒否するには**  
次の `reject-invitation` の例では、動作グラフ arn:aws:detective:us-east-1:111122223333:graph:123412341234 のメンバーアカウントになるための招待を拒否します。  

```
aws detective reject-invitation \
    --graph-arn arn:aws:detective:us-east-1:111122223333:graph:123412341234
```
このコマンドでは何も出力されません。  
詳細については、「*Amazon Detective Administration Guide*」の「Responding to a behavior graph invitation<https://docs.aws.amazon.com/detective/latest/adminguide/member-invitation-response.html>」を参照してください。  
+  API の詳細については、「*AWS CLI コマンドリファレンス*」の「[RejectInvitation](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/detective/reject-invitation.html)」を参照してください。

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

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

**AWS CLI**  
**リソースにタグを割り当てるには**  
次の `tag-resource` の例では、Department タグの値を指定された動作グラフに割り当てます。  

```
aws detective tag-resource \
    --resource-arn arn:aws:detective:us-east-1:111122223333:graph:123412341234 \
    --tags '{"Department":"Finance"}'
```
このコマンドでは何も出力されません。  
詳細については、「*Amazon Detective Administration Guide*」の「[Managing tags for a behavior graph](https://docs.aws.amazon.com/detective/latest/adminguide/graph-tags.html)」を参照してください。  
+  API の詳細については、「*AWS CLI コマンドリファレンス*」の「[TagResource](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/detective/tag-resource.html)」を参照してください。

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

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

**AWS CLI**  
**リソースからタグ値を削除するには**  
次の `untag-resource` の例では、指定された動作グラフから Department タグを削除します。  

```
aws detective untag-resource \
    --resource-arn arn:aws:detective:us-east-1:111122223333:graph:123412341234 \
    --tag-keys "Department"
```
このコマンドでは何も出力されません。  
詳細については、「*Amazon Detective Administration Guide*」の「[Managing tags for a behavior graph](https://docs.aws.amazon.com/detective/latest/adminguide/graph-tags.html)」を参照してください。  
+  API の詳細については、「AWS CLI コマンドリファレンス」の「[UntagResource](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/detective/untag-resource.html)」を参照してください。