

# AWS WAF Classic Regional使用 的 示例AWS CLI
<a name="cli_waf-regional_code_examples"></a>

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

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

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

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

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

### `associate-web-acl`
<a name="waf-regional_AssociateWebAcl_cli_topic"></a>

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

**AWS CLI**  
**将 Web ACL 与资源相关联**  
以下 `associate-web-acl` 命令将由 web-acl-id 指定的 Web ACL 与 resource-arn 指定的资源相关联。资源 ARN 可以指应用程序负载均衡器或 API Gateway：  

```
aws waf-regional associate-web-acl \
    --web-acl-id a123fae4-b567-8e90-1234-5ab67ac8ca90 \
    --resource-arn 12cs345-67cd-890b-1cd2-c3a4567d89f1
```
有关更多信息，请参阅《AWS WAF 开发人员指南》**中的[使用 Web ACL](https://docs.aws.amazon.com/waf/latest/developerguide/web-acl-working-with.html)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [AssociateWebAcl](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/waf-regional/associate-web-acl.html)。

### `put-logging-configuration`
<a name="waf-regional_PutLoggingConfiguration_cli_topic"></a>

以下代码示例演示了如何使用 `put-logging-configuration`。

**AWS CLI**  
**使用指定的 Kinesis Firehose 流 ARN 为 Web ACL ARN 创建日志记录配置**  
以下 `put-logging-configuration` 示例显示 `us-east-1` 区域中具有 ALB/APIGateway 的 WAF 日志记录配置。  

```
aws waf-regional put-logging-configuration \
    --logging-configuration ResourceArn=arn:aws:waf-regional:us-east-1:123456789012:webacl/3bffd3ed-fa2e-445e-869f-a6a7cf153fd3,LogDestinationConfigs=arn:aws:firehose:us-east-1:123456789012:deliverystream/aws-waf-logs-firehose-stream,RedactedFields=[] \
    --region us-east-1
```
输出：  

```
{
    "LoggingConfiguration": {
        "ResourceArn": "arn:aws:waf-regional:us-east-1:123456789012:webacl/3bffd3ed-fa2e-445e-869f-a6a7cf153fd3",
        "LogDestinationConfigs": [
            "arn:aws:firehose:us-east-1:123456789012:deliverystream/aws-waf-logs-firehose-stream"
        ]
    }
}
```
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [PutLoggingConfiguration](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/waf-regional/put-logging-configuration.html)。

### `update-byte-match-set`
<a name="waf-regional_UpdateByteMatchSet_cli_topic"></a>

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

**AWS CLI**  
**更新字节匹配集**  
以下 `update-byte-match-set` 命令删除 `ByteMatchSet` 中的 `ByteMatchTuple` 对象（筛选条件）。由于该 `updates` 值嵌入了双引号，因此必须用单引号将该值括起来。  

```
aws waf-regional update-byte-match-set \
    --byte-match-set-id a123fae4-b567-8e90-1234-5ab67ac8ca90 \
    --change-token 12cs345-67cd-890b-1cd2-c3a4567d89f1 \
    --updates 'Action="DELETE",ByteMatchTuple={FieldToMatch={Type="HEADER",Data="referer"},TargetString="badrefer1",TextTransformation="NONE",PositionalConstraint="CONTAINS"}'
```
有关更多信息，请参阅《AWS WAF 开发人员指南》**中的[使用字符串匹配条件](https://docs.aws.amazon.com/waf/latest/developerguide/web-acl-string-conditions.html)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [UpdateByteMatchSet](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/waf-regional/update-byte-match-set.html)。

### `update-ip-set`
<a name="waf-regional_UpdateIpSet_cli_topic"></a>

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

**AWS CLI**  
**更新 IP 集**  
以下 `update-ip-set` 命令使用 IPv4 地址更新 IPSet 并删除 IPv6 地址。通过运行 `get-change-token` 命令获取 `change-token` 的值。由于更新值包含嵌入式双引号，因此必须用单引号将该值括起来。  

```
aws waf update-ip-set \
    --ip-set-id a123fae4-b567-8e90-1234-5ab67ac8ca90 \
    --change-token 12cs345-67cd-890b-1cd2-c3a4567d89f1 \
    --updates 'Action="INSERT",IPSetDescriptor={Type="IPV4",Value="12.34.56.78/16"},Action="DELETE",IPSetDescriptor={Type="IPV6",Value="1111:0000:0000:0000:0000:0000:0000:0111/128"}'
```
或者，也可以使用 JSON 文件来指定输入。例如：  

```
aws waf-regional update-ip-set \
    --ip-set-id a123fae4-b567-8e90-1234-5ab67ac8ca90 \
    --change-token 12cs345-67cd-890b-1cd2-c3a4567d89f1  \
    --updates file://change.json
```
`change.json` 的内容  

```
[
    {
        "Action": "INSERT",
        "IPSetDescriptor":
        {
            "Type": "IPV4",
            "Value": "12.34.56.78/16"
        }
    },
    {
        "Action": "DELETE",
        "IPSetDescriptor":
        {
            "Type": "IPV6",
            "Value": "1111:0000:0000:0000:0000:0000:0000:0111/128"
        }
    }
]
```
有关更多信息，请参阅《AWS WAF 开发人员指南》**中的[使用 IP 匹配条件](https://docs.aws.amazon.com/waf/latest/developerguide/web-acl-ip-conditions.html)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》中的 [UpdateIpSet](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/waf-regional/update-ip-set.html)**。

### `update-rule`
<a name="waf-regional_UpdateRule_cli_topic"></a>

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

**AWS CLI**  
**更新规则**  
以下 `update-rule` 命令删除规则中的 `Predicate` 对象。由于 `updates` 值嵌入了双引号，因此必须用单引号将整个值括起来。  

```
aws waf-regional update-rule \
    --rule-id a123fae4-b567-8e90-1234-5ab67ac8ca90 \
    --change-token 12cs345-67cd-890b-1cd2-c3a4567d89f1 \
    --updates 'Action="DELETE",Predicate={Negated=false,Type="ByteMatch",DataId="MyByteMatchSetID"}'
```
有关更多信息，请参阅《AWS WAF 开发人员指南》中的[使用规则](https://docs.aws.amazon.com/waf/latest/developerguide/web-acl-rules.html)**。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [UpdateRule](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/waf-regional/update-rule.html)。

### `update-size-constraint-set`
<a name="waf-regional_UpdateSizeConstraintSet_cli_topic"></a>

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

**AWS CLI**  
**更新大小约束集**  
以下 `update-size-constraint-set` 命令删除大小约束集中的 SizeConstraint 对象（筛选条件）。由于 `updates` 值包含嵌入式双引号，因此必须用单引号将整个值括起来。  

```
aws waf-regional update-size-constraint-set \
    --size-constraint-set-id a123fae4-b567-8e90-1234-5ab67ac8ca90 \
    --change-token 12cs345-67cd-890b-1cd2-c3a4567d89f1 \
    --updates 'Action="DELETE",SizeConstraint={FieldToMatch={Type="QUERY_STRING"},TextTransformation="NONE",ComparisonOperator="GT",Size=0}'
```
有关更多信息，请参阅《AWS WAF 开发人员指南》**中的[使用大小约束条件](https://docs.aws.amazon.com/waf/latest/developerguide/web-acl-size-conditions.html)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [UpdateSizeConstraintSet](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/waf-regional/update-size-constraint-set.html)。

### `update-sql-injection-match-set`
<a name="waf-regional_UpdateSqlInjectionMatchSet_cli_topic"></a>

以下代码示例演示了如何使用 `update-sql-injection-match-set`。

**AWS CLI**  
**更新 SQL 注入匹配集**  
以下 `update-sql-injection-match-set` 命令删除 SQL 注入匹配集中的 `SqlInjectionMatchTuple` 对象（筛选条件）。由于 `updates` 值包含嵌入式双引号，因此必须用单引号将整个值括起来。  
aws waf-regional update-sql-injection-match-set --sql-injection-match-set-id a123fae4-b567-8e90-1234-5ab67ac8ca90 --change-token 12cs345-67cd-890b-1cd2-c3a4567d89f1 --updates 'Action="DELETE",SqlInjectionMatchTuple=\$1FieldToMatch=\$1Type="QUERY\$1STRING"\$1,TextTransformation="URL\$1DECODE"\$1'  
有关更多信息，请参阅《AWS WAF 开发人员指南》**中的[使用 SQL 注入匹配条件](https://docs.aws.amazon.com/waf/latest/developerguide/web-acl-sql-conditions.html)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [UpdateSqlInjectionMatchSet](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/waf-regional/update-sql-injection-match-set.html)。

### `update-web-acl`
<a name="waf-regional_UpdateWebAcl_cli_topic"></a>

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

**AWS CLI**  
**更新 Web ACL**  
以下 `update-web-acl` 命令删除 Web ACL 中的 `ActivatedRule` 对象。由于 `updates` 值包含嵌入式双引号，因此必须用单引号将整个值括起来。  

```
aws waf-regional update-web-acl \
    --web-acl-id a123fae4-b567-8e90-1234-5ab67ac8ca90 \
    --change-token 12cs345-67cd-890b-1cd2-c3a4567d89f1 \
    --updates Action="DELETE",ActivatedRule='{Priority=1,RuleId="WAFRule-1-Example",Action={Type="ALLOW"},Type="ALLOW"}'
```
有关更多信息，请参阅《AWS WAF 开发人员指南》**中的[使用 Web ACL](https://docs.aws.amazon.com/waf/latest/developerguide/web-acl-working-with.html)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [UpdateWebAcl](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/waf-regional/update-web-acl.html)。

### `update-xss-match-set`
<a name="waf-regional_UpdateXssMatchSet_cli_topic"></a>

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

**AWS CLI**  
**更新 XSSMatchSet**  
以下 `update-xss-match-set` 命令删除 `XssMatchSet` 中的 `XssMatchTuple` 对象（筛选条件）。由于 `updates` 值包含嵌入式双引号，因此必须用单引号将整个值括起来。  

```
aws waf-regional update-xss-match-set \
    --xss-match-set-id a123fae4-b567-8e90-1234-5ab67ac8ca90 \
    --change-token 12cs345-67cd-890b-1cd2-c3a4567d89f1 \
    --updates 'Action="DELETE",XssMatchTuple={FieldToMatch={Type="QUERY_STRING"},TextTransformation="URL_DECODE"}'
```
有关更多信息，请参阅《AWS WAF 开发人员指南》**中的[使用跨站点脚本匹配条件](https://docs.aws.amazon.com/waf/latest/developerguide/web-acl-xss-conditions.html)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [UpdateXssMatchSet](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/waf-regional/update-xss-match-set.html)。