

文档 AWS SDK 示例 GitHub 存储库中还有更多 [S AWS DK 示例](https://github.com/awsdocs/aws-doc-sdk-examples)。

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用 Shield 示例 AWS CLI
<a name="cli_2_shield_code_examples"></a>

以下代码示例向您展示了如何使用 with Shield 来执行操作和实现常见场景。 AWS Command Line Interface 

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

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

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

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

### `associate-drt-log-bucket`
<a name="shield_AssociateDrtLogBucket_cli_2_topic"></a>

以下代码示例演示了如何使用 `associate-drt-log-bucket`。

**AWS CLI**  
**授权 DRT 访问 Amazon S3 存储桶**  
以下 `associate-drt-log-bucket` 示例在 DRT 和指定的 S3 存储桶之间创建关联。这允许 DRT 代表账户访问存储桶：  

```
aws shield associate-drt-log-bucket \
    --log-bucket flow-logs-for-website-lb
```
此命令不生成任何输出。  
有关更多信息，请参阅 *AWS Shield 高级开发者指南*中的[授权 DDo S 响应小组](https://docs.aws.amazon.com/waf/latest/developerguide/authorize-DRT.html)。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[AssociateDrtLogBucket](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/shield/associate-drt-log-bucket.html)*中的。

### `associate-drt-role`
<a name="shield_AssociateDrtRole_cli_2_topic"></a>

以下代码示例演示了如何使用 `associate-drt-role`。

**AWS CLI**  
**授权 DRT 代表您缓解潜在攻击**  
以下 `associate-drt-role` 示例在 DRT 和指定角色之间创建关联。DRT 可以使用该角色访问和管理账户。  

```
aws shield associate-drt-role \
    --role-arn arn:aws:iam::123456789012:role/service-role/DrtRole
```
此命令不生成任何输出。  
有关更多信息，请参阅 *AWS Shield 高级开发者指南*中的[授权 DDo S 响应小组](https://docs.aws.amazon.com/waf/latest/developerguide/authorize-DRT.html)。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[AssociateDrtRole](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/shield/associate-drt-role.html)*中的。

### `create-protection`
<a name="shield_CreateProtection_cli_2_topic"></a>

以下代码示例演示了如何使用 `create-protection`。

**AWS CLI**  
**为单个 AWS 资源启用 AWS Shield 高级保护**  
以下`create-protection`示例为指定 AWS CloudFront 发行版启用 Shield 高级保护。  

```
aws shield create-protection \
    --name "Protection for CloudFront distribution" \
    --resource-arn arn:aws:cloudfront::123456789012:distribution/E198WC25FXOWY8
```
输出：  

```
{
    "ProtectionId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111"
}
```
有关更多信息，请参阅《AWS Shield Advanced 开发人员指南》**中的[指定要保护的资源](https://docs.aws.amazon.com/waf/latest/developerguide/ddos-choose-resources.html)。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[CreateProtection](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/shield/create-protection.html)*中的。

### `create-subscription`
<a name="shield_CreateSubscription_cli_2_topic"></a>

以下代码示例演示了如何使用 `create-subscription`。

**AWS CLI**  
**为账户启用 AWS Shield 高级防护**  
以下 `create-subscription` 示例为账户启用 Shield Advanced 保护。  

```
aws shield create-subscription
```
此命令不生成任何输出。  
有关更多信息，请参阅《[AWS Shield 高级*开发者指南》中的AWS Shield 高级版*入门](https://docs.aws.amazon.com/waf/latest/developerguide/getting-started-ddos.html)。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[CreateSubscription](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/shield/create-subscription.html)*中的。

### `delete-protection`
<a name="shield_DeleteProtection_cli_2_topic"></a>

以下代码示例演示了如何使用 `delete-protection`。

**AWS CLI**  
**从 AWS 资源中移除 AWS Shield 高级防护**  
以下`delete-protection`示例移除了指定的 AWS Shield 高级保护。  

```
aws shield delete-protection \
    --protection-id a1b2c3d4-5678-90ab-cdef-EXAMPLE11111
```
此命令不生成任何输出。  
有关更多信息，请参阅《[AWS Shield 高级开发者指南》中的从 AWS 资源中移除AWS](https://docs.aws.amazon.com/waf/latest/developerguide/remove-protection.html) *Shield Advance* d。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[DeleteProtection](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/shield/delete-protection.html)*中的。

### `describe-attack`
<a name="shield_DescribeAttack_cli_2_topic"></a>

以下代码示例演示了如何使用 `describe-attack`。

**AWS CLI**  
**检索攻击的详细描述**  
以下`describe-attack`示例显示有关具有指定攻击 ID 的 DDo S 攻击的详细信息。你可以 IDs 通过运行`list-attacks`命令来获得攻击。  

```
aws shield describe-attack --attack-id a1b2c3d4-5678-90ab-cdef-EXAMPLE22222
```
输出：  

```
{
    "Attack": {
        "AttackId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE22222",
        "ResourceArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/testElb",
        "SubResources": [
            {
                "Type": "IP",
                "Id": "192.0.2.2",
                "AttackVectors": [
                    {
                        "VectorType": "SYN_FLOOD",
                        "VectorCounters": [
                            {
                                "Name": "SYN_FLOOD_BPS",
                                "Max": 982184.0,
                                "Average": 982184.0,
                                "Sum": 11786208.0,
                                "N": 12,
                                "Unit": "BPS"
                            }
                        ]
                    }
                ],
                "Counters": []
            },
            {
                "Type": "IP",
                "Id": "192.0.2.3",
                "AttackVectors": [
                    {
                        "VectorType": "SYN_FLOOD",
                        "VectorCounters": [
                            {
                                "Name": "SYN_FLOOD_BPS",
                                "Max": 982184.0,
                                "Average": 982184.0,
                                "Sum": 9821840.0,
                                "N": 10,
                                "Unit": "BPS"
                            }
                        ]
                    }
                ],
                "Counters": []
            },
            {
                "Type": "IP",
                "Id": "192.0.2.4",
                "AttackVectors": [
                    {
                        "VectorType": "SYN_FLOOD",
                        "VectorCounters": [
                            {
                                "Name": "SYN_FLOOD_BPS",
                                "Max": 982184.0,
                                "Average": 982184.0,
                                "Sum": 7857472.0,
                                "N": 8,
                                "Unit": "BPS"
                            }
                        ]
                    }
                ],
                "Counters": []
            },
            {
                "Type": "IP",
                "Id": "192.0.2.5",
                "AttackVectors": [
                    {
                        "VectorType": "SYN_FLOOD",
                        "VectorCounters": [
                            {
                                "Name": "SYN_FLOOD_BPS",
                                "Max": 982184.0,
                                "Average": 982184.0,
                                "Sum": 1964368.0,
                                "N": 2,
                                "Unit": "BPS"
                            }
                        ]
                    }
                ],
                "Counters": []
            },
            {
                "Type": "IP",
                "Id": "2001:DB8::bcde:4321:8765:0:0",
                "AttackVectors": [
                    {
                        "VectorType": "SYN_FLOOD",
                        "VectorCounters": [
                            {
                                "Name": "SYN_FLOOD_BPS",
                                "Max": 982184.0,
                                "Average": 982184.0,
                                "Sum": 1964368.0,
                                "N": 2,
                                "Unit": "BPS"
                            }
                        ]
                    }
                ],
                "Counters": []
            },
            {
                "Type": "IP",
                "Id": "192.0.2.6",
                "AttackVectors": [
                    {
                        "VectorType": "SYN_FLOOD",
                        "VectorCounters": [
                            {
                                "Name": "SYN_FLOOD_BPS",
                                "Max": 982184.0,
                                "Average": 982184.0,
                                "Sum": 1964368.0,
                                "N": 2,
                                "Unit": "BPS"
                            }
                        ]
                    }
                ],
                "Counters": []
            }
        ],
        "StartTime": 1576024927.457,
        "EndTime": 1576025647.457,
        "AttackCounters": [],
        "AttackProperties": [
            {
                "AttackLayer": "NETWORK",
                "AttackPropertyIdentifier": "SOURCE_IP_ADDRESS",
                "TopContributors": [
                    {
                        "Name": "198.51.100.5",
                        "Value": 2024475682
                    },
                    {
                        "Name": "198.51.100.8",
                        "Value": 1311380863
                    },
                    {
                        "Name": "203.0.113.4",
                        "Value": 900599855
                    },
                    {
                        "Name": "198.51.100.4",
                        "Value": 769417366
                    },
                    {
                        "Name": "203.1.113.13",
                        "Value": 757992847
                    }
                ],
                "Unit": "BYTES",
                "Total": 92773354841
            },
            {
                "AttackLayer": "NETWORK",
                "AttackPropertyIdentifier": "SOURCE_COUNTRY",
                "TopContributors": [
                    {
                        "Name": "United States",
                        "Value": 80938161764
                    },
                    {
                        "Name": "Brazil",
                        "Value": 9929864330
                    },
                    {
                        "Name": "Netherlands",
                        "Value": 1635009446
                    },
                    {
                        "Name": "Mexico",
                        "Value": 144832971
                    },
                    {
                        "Name": "Japan",
                        "Value": 45369000
                    }
                ],
                "Unit": "BYTES",
                "Total": 92773354841
            },
            {
                "AttackLayer": "NETWORK",
                "AttackPropertyIdentifier": "SOURCE_ASN",
                "TopContributors": [
                    {
                        "Name": "12345",
                        "Value": 74953625841
                    },
                    {
                        "Name": "12346",
                        "Value": 4440087595
                    },
                    {
                        "Name": "12347",
                        "Value": 1635009446
                    },
                    {
                        "Name": "12348",
                        "Value": 1221230000
                    },
                    {
                        "Name": "12349",
                        "Value": 1199425294
                    }
                ],
                "Unit": "BYTES",
                "Total": 92755479921
            }
        ],
        "Mitigations": []
    }
}
```
有关更多信息，请参阅《*AWS Shield 高级开发者指南*》中的 “[查看 DDo S 事件](https://docs.aws.amazon.com/waf/latest/developerguide/using-ddos-reports.html)”。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[DescribeAttack](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/shield/describe-attack.html)*中的。

### `describe-drt-access`
<a name="shield_DescribeDrtAccess_cli_2_topic"></a>

以下代码示例演示了如何使用 `describe-drt-access`。

**AWS CLI**  
**检索 DRT 代表您缓解攻击的授权说明**  
以下 `describe-drt-access` 示例检索 DRT 拥有的角色和 S3 存储桶授权，这使其能够代表您应对潜在的攻击。  

```
aws shield describe-drt-access
```
输出：  

```
{
    "RoleArn": "arn:aws:iam::123456789012:role/service-role/DrtRole",
    "LogBucketList": [
        "flow-logs-for-website-lb"
    ]
}
```
有关更多信息，请参阅 *AWS Shield 高级开发者指南*中的[授权 DDo S 响应小组](https://docs.aws.amazon.com/waf/latest/developerguide/authorize-DRT.html)。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[DescribeDrtAccess](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/shield/describe-drt-access.html)*中的。

### `describe-emergency-contact-settings`
<a name="shield_DescribeEmergencyContactSettings_cli_2_topic"></a>

以下代码示例演示了如何使用 `describe-emergency-contact-settings`。

**AWS CLI**  
**检索您在 DRT 存档的紧急电子邮件地址**  
以下 `describe-emergency-contact-settings` 示例检索 DRT 存档的账户电子邮件地址。这些是 DRT 在应对可疑攻击时应联系的地址。  

```
aws shield describe-emergency-contact-settings
```
输出：  

```
{
    "EmergencyContactList": [
        {
            "EmailAddress": "ops@example.com"
        },
        {
            "EmailAddress": "ddos-notifications@example.com"
       }
    ]
}
```
*有关更多信息，请参阅 Shield 高级开发者指南中的 AWS Shield 的工作原理< https://docs.aws.amazon.com/waf/ latest/developerguide/ddos-overview.html>。AWS *  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[DescribeEmergencyContactSettings](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/shield/describe-emergency-contact-settings.html)*中的。

### `describe-protection`
<a name="shield_DescribeProtection_cli_2_topic"></a>

以下代码示例演示了如何使用 `describe-protection`。

**AWS CLI**  
**检索 AWS Shield 高级防护的详细信息**  
以下 `describe-protection` 示例显示指定 ID 的 Shield Advanced 保护的详细信息。您可以 IDs 通过运行`list-protections`命令来获得保护。  

```
aws shield describe-protection \
    --protection-id a1b2c3d4-5678-90ab-cdef-EXAMPLE11111
```
输出：  

```
{
    "Protection": {
        "Id": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111",
        "Name": "1.2.3.4",
        "ResourceArn": "arn:aws:ec2:us-west-2:123456789012:eip-allocation/eipalloc-0ac1537af40742a6d"
    }
}
```
有关更多信息，请参阅《AWS Shield Advanced 开发人员指南》**中的[指定要保护的资源](https://docs.aws.amazon.com/waf/latest/developerguide/ddos-choose-resources.html)。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[DescribeProtection](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/shield/describe-protection.html)*中的。

### `describe-subscription`
<a name="shield_DescribeSubscription_cli_2_topic"></a>

以下代码示例演示了如何使用 `describe-subscription`。

**AWS CLI**  
**检索账户的 AWS Shield 高级保护的详细信息**  
以下 `describe-subscription` 示例显示为账户提供的 Shield Advanced 保护的详细信息：  

```
aws shield describe-subscription
```
输出：  

```
{
    "Subscription": {
        "StartTime": 1534368978.0,
        "EndTime": 1597613778.0,
        "TimeCommitmentInSeconds": 63244800,
        "AutoRenew": "ENABLED",
        "Limits": [
            {
                "Type": "GLOBAL_ACCELERATOR",
                "Max": 1000
            },
            {
                "Type": "ROUTE53_HOSTED_ZONE",
                "Max": 1000
            },
            {
                "Type": "CF_DISTRIBUTION",
                "Max": 1000
            },
            {
                "Type": "ELB_LOAD_BALANCER",
                "Max": 1000
            },
            {
                "Type": "EC2_ELASTIC_IP_ALLOCATION",
                "Max": 1000
            }
        ]
    }
}
```
有关更多信息，[请参阅《 AWS Shield *高级开发者指南》中的AWS Shield* 工作原理](https://docs.aws.amazon.com/waf/latest/developerguide/ddos-overview.html)。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[DescribeSubscription](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/shield/describe-subscription.html)*中的。

### `disassociate-drt-log-bucket`
<a name="shield_DisassociateDrtLogBucket_cli_2_topic"></a>

以下代码示例演示了如何使用 `disassociate-drt-log-bucket`。

**AWS CLI**  
**移除 DRT 代表您访问 Amazon S3 存储桶的授权**  
以下 `disassociate-drt-log-bucket` 示例移除 DRT 与指定 S3 存储桶之间的关联。在此命令完成后，DRT 将无法再代表该账户访问该存储桶。  

```
aws shield disassociate-drt-log-bucket \
    --log-bucket flow-logs-for-website-lb
```
此命令不生成任何输出。  
有关更多信息，请参阅 *AWS Shield 高级开发者指南*中的[授权 DDo S 响应小组](https://docs.aws.amazon.com/waf/latest/developerguide/authorize-DRT.html)。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[DisassociateDrtLogBucket](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/shield/disassociate-drt-log-bucket.html)*中的。

### `disassociate-drt-role`
<a name="shield_DisassociateDrtRole_cli_2_topic"></a>

以下代码示例演示了如何使用 `disassociate-drt-role`。

**AWS CLI**  
**移除 DRT 代表您缓解潜在攻击的授权**  
以下 `disassociate-drt-role` 示例移除 DRT 与账户之间的关联。该调用结束后，DRT 将无法再访问或管理您的帐户。  

```
aws shield disassociate-drt-role
```
此命令不生成任何输出。  
有关更多信息，请参阅 *AWS Shield 高级开发者指南*中的[授权 DDo S 响应小组](https://docs.aws.amazon.com/waf/latest/developerguide/authorize-DRT.html)。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[DisassociateDrtRole](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/shield/disassociate-drt-role.html)*中的。

### `get-subscription-state`
<a name="shield_GetSubscriptionState_cli_2_topic"></a>

以下代码示例演示了如何使用 `get-subscription-state`。

**AWS CLI**  
**检索账户 AWS Shield Advanced 订阅的当前状态**  
以下 `get-subscription-state` 示例检索账户 Shield Advanced 保护的状态。  

```
aws shield get-subscription-state
```
输出：  

```
{
    "SubscriptionState": "ACTIVE"
}
```
有关更多信息，[请参阅《 AWS Shield *高级开发者指南》中的AWS Shield* 工作原理](https://docs.aws.amazon.com/waf/latest/developerguide/ddos-overview.html)。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[GetSubscriptionState](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/shield/get-subscription-state.html)*中的。

### `list-attacks`
<a name="shield_ListAttacks_cli_2_topic"></a>

以下代码示例演示了如何使用 `list-attacks`。

**AWS CLI**  
**从 AWS Shield Advanced 中检索攻击摘要**  
以下`list-attacks`示例检索指定 AWS CloudFront 分布在指定时间段内的攻击摘要。响应包括攻击 IDs ，您可以向`describe-attack`命令提供这些攻击，以获取有关攻击的详细信息。  

```
aws shield list-attacks \
    --resource-arns arn:aws:cloudfront::12345678910:distribution/E1PXMP22ZVFAOR \
    --start-time FromInclusive=1529280000,ToExclusive=1529300000
```
输出：  

```
{
    "AttackSummaries": [
        {
            "AttackId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111",
            "ResourceArn": "arn:aws:cloudfront::123456789012:distribution/E1PXMP22ZVFAOR",
            "StartTime": 1529280000.0,
            "EndTime": 1529449200.0,
            "AttackVectors": [
                {
                    "VectorType": "SYN_FLOOD"
                }
            ]
        }
    ]
}
```
有关更多信息，请参阅《*AWS Shield 高级开发者指南*》中的 “[查看 DDo S 事件](https://docs.aws.amazon.com/waf/latest/developerguide/using-ddos-reports.html)”。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[ListAttacks](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/shield/list-attacks.html)*中的。

### `list-protections`
<a name="shield_ListProtections_cli_2_topic"></a>

以下代码示例演示了如何使用 `list-protections`。

**AWS CLI**  
**从 Shi AWS eld Advanced 中检索防护摘要**  
以下 `list-protections` 示例检索为该账户启用的保护的摘要。  

```
aws shield list-protections
```
输出：  

```
{
    "Protections": [
        {
            "Id": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111",
            "Name": "Protection for CloudFront distribution",
            "ResourceArn": "arn:aws:cloudfront::123456789012:distribution/E198WC25FXOWY8"
        }
    ]
}
```
有关更多信息，请参阅《AWS Shield Advanced 开发人员指南》**中的[指定要保护的资源](https://docs.aws.amazon.com/waf/latest/developerguide/ddos-choose-resources.html)。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[ListProtections](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/shield/list-protections.html)*中的。

### `update-emergency-contact-settings`
<a name="shield_UpdateEmergencyContactSettings_cli_2_topic"></a>

以下代码示例演示了如何使用 `update-emergency-contact-settings`。

**AWS CLI**  
**定义 DRT 存档中的紧急电子邮件地址**  
以下 `update-emergency-contact-settings` 示例定义 DRT 在响应可疑攻击时应联系的两个电子邮件地址。  

```
aws shield update-emergency-contact-settings \
        --emergency-contact-list EmailAddress=ops@example.com EmailAddress=ddos-notifications@example.com
```
此命令不生成任何输出。  
有关更多信息，[请参阅《 AWS Shield *高级开发者指南》中的AWS Shield* 工作原理](https://docs.aws.amazon.com/waf/latest/developerguide/ddos-overview.html)。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[UpdateEmergencyContactSettings](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/shield/update-emergency-contact-settings.html)*中的。

### `update-subscription`
<a name="shield_UpdateSubscription_cli_2_topic"></a>

以下代码示例演示了如何使用 `update-subscription`。

**AWS CLI**  
**修改账户的 AWS Shield Advanced 订阅**  
以下`update-subscription`示例为该账户启用了 AWS Shield Advanced 订阅的自动续订。  

```
aws shield update-subscription \
    --auto-renew ENABLED
```
此命令不生成任何输出。  
有关更多信息，[请参阅《 AWS Shield *高级开发者指南》中的AWS Shield* 工作原理](https://docs.aws.amazon.com/waf/latest/developerguide/ddos-overview.html)。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[UpdateSubscription](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/shield/update-subscription.html)*中的。