

# 使用 AWS CLI 的 Detective 示例
<a name="cli_detective_code_examples"></a>

以下代码示例演示如何通过将 AWS Command Line Interface与 Detective 结合使用，来执行操作和实现常见场景。

*操作是大型程序的代码摘录*，必须在上下文中运行。您可以通过操作了解如何调用单个服务函数，还可以通过函数相关场景的上下文查看操作。

每个示例都包含一个指向完整源代码的链接，您可以从中找到有关如何在上下文中设置和运行代码的说明。

**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 管理指南》**中的[响应行为图邀请](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。创建一个新的行为图，将该账户作为管理员账户。该命令还将值 Finance 分配给 Department 标签。  

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

```
{
    "GraphArn": "arn:aws:detective:us-east-1:111122223333:graph:027c7c4610ea4aacaf0b883093cab899"
}
```
有关更多信息，请参阅《Amazon Detective 管理指南》**中的[启用 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` 示例邀请两个 AWS 账户成为行为图 arn:aws:detective:us-east-1:111122223333:graph:123412341234 中的成员账户。对于每个账户，请求会提供 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 管理指南》**中的“邀请成员账户加入行为图<https://docs.aws.amazon.com/detective/latest/adminguide/graph-admin-add-member-accounts.html>”。  
**在无需发送邀请电子邮件的情况下邀请成员账户**  
以下 `create-members` 示例邀请两个 AWS 账户成为行为图 arn:aws:detective:us-east-1:111122223333:graph:123412341234 中的成员账户。对于每个账户，请求会提供 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 管理指南》**中的“邀请成员账户加入行为图<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 管理指南》**中的[禁用 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 中移除两个成员账户。为了识别账户，该请求提供了 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 管理指南》**中的“从行为图中移除成员账户<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**  
**放弃行为图成员资格**  
以下 disassociate-membership 示例从行为图 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 管理指南》**中的“从行为图中移除账户<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 中两个成员账户的信息。对于这两个账户，该请求提供了 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 管理指南》**中的“查看行为图中账户的列表<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 管理指南》**中的“查看行为图邀请的列表<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 管理指南》**中的[查看行为图中账户的列表](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 管理指南》**中的[管理行为图的标签](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 管理指南》**中的“响应行为图邀请<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 管理指南》**中的[管理行为图的标签](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 管理指南》**中的[管理行为图的标签](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)。