

# 使用路由规则重新创建 API 映射
<a name="rest-api-routing-rules-recreate-api-mapping"></a>

可以使用路由规则重新创建 API 映射。要重新创建 API 映射，请确保开启基本路径删除。这将保留 API 映射的行为。有关更多信息，请参阅 [使用基本路径条件删除基本路径](rest-api-routing-rules.md#rest-api-routing-rules-condition-path-split)。

以下教程展示了如何将 API 映射 `https:// api.example.com/orders/v2/items/categories/5` 重新创建为路由规则，以及如何更新访问日志以记录 API Gateway 用于向 API 发送流量的路由规则 ID。

------
#### [ AWS 管理控制台 ]

**将路由模式设置为 ROUTING\$1RULE\$1THEN\$1API\$1MAPPING**

1. 通过以下网址登录到 Amazon API Gateway 控制台：[https://console.aws.amazon.com/apigateway](https://console.aws.amazon.com/apigateway)。

1. 从主导航窗格中选择**自定义域名**。

1. 选择自定义域名。

1. 对于**域详细信息**，选择**编辑**。

1. 对于**路由模式**，请选择 **ROUTING\$1RULE\$1THEN\$1API\$1MAPPING**。

1. 选择**保存** 

设置路由模式后，可以创建路由规则。

**创建路由规则**

1. 在**路由详情**选项卡上，选择**添加路由规则**。

1. 选择**添加新条件**，然后选择**路径**。

1. 对于**路径**，输入 **orders/v2/items/categories/5**。

1. 对于**删除基本路径**，选择**活动**。

1. 对于**目标 API**，请选择目标 API。

1. 对于**目标阶段**，选择目标阶段。

1. 选择**下一步**。

1. 对于优先级，请输入优先级。

   即使您保留现有的 API 映射，API Gateway 也将始终使用新的路由规则，因为路由规则始终优先于 API 映射。

1. 选择**保存更改**。

创建路由规则后，更新阶段的访问日志格式或创建新日志，以确认 API Gateway 使用路由规则来将流量发送到 API。

**更新访问日志**

1. 通过以下网址登录到 Amazon API Gateway 控制台：[https://console.aws.amazon.com/apigateway](https://console.aws.amazon.com/apigateway)。

1. 选择 API。

1. 在主导航窗格中，选择**阶段**。

1. 对于**日志和跟踪**，选择**编辑**。

   如果您没有日志组，请参阅[为 API Gateway 中的 REST API 设置 CloudWatch 日志记录](set-up-logging.md)。

1. 将 **\$1context.customDomain.routingRuleIdMatched** 添加到您的日志格式。

   此日志组记录 API Gateway 用来向 API 发送流量的路由规则 ID。有关更多信息，请参阅 [我不知道 API Gateway 是如何将流量发送到 API 的](rest-api-routing-rules-troubleshoot.md#rest-api-routing-rules-logging)。

1. 选择 **Save**。

更新访问日志后，调用自定义域名。以下是一个示例 curl 命令，用于调用基本路径为 `orders/v2/items/categories/5` 的自定义域名 `https://api.example.com`。

```
curl "https://api.example.com/orders/v2/items/categories/5"
```

成功调用自定义域名后，请确认 CloudWatch Logs 显示 `routingRuleIdMatched`。要了解如何使用 CloudWatch Logs 控制台来查看日志组，请参阅[在 CloudWatch 控制台中查看 API Gateway 日志事件](view-cloudwatch-log-events-in-cloudwatch-console.md)。

------
#### [ AWS CLI ]

1. 使用以下 [update-domain-name](https://docs.aws.amazon.com/cli/latest/reference/apigatewayv2/update-domain-name.html) 命令更新域名 `api.example.com` 以使用路由模式 `ROUTING_RULE_THEN_API_MAPPING`。

   ```
   aws apigatewayv2 update-domain-name \
     --domain-name 'api.example.com' \
     --routing-mode ROUTING_RULE_THEN_API_MAPPING
   ```

1. 使用以下 [create-routing-rule](https://docs.aws.amazon.com/cli/latest/reference/apigatewayv2/create-routing-rule.html) 命令创建新的路由规则，以重新创建 API 映射 `https://api.example.com/orders/v2/items/categories/5`。

   ```
   aws apigatewayv2 create-routing-rule \
     --domain-name 'api.example.com' \
     --priority 50 \
     --conditions '[
     {
       "MatchBasePaths": {
         "AnyOf": [
           "orders/v2/items/categories/5"
         ]
       }
     }
   ]' \
     --actions '[
     {
       "InvokeApi": {
         "ApiId": "a1b2c3",
         "Stage": "prod",
         "StripBasePath": true
       }
     }
   ]'
   ```

1. 使用以下 [update-stage](https://docs.aws.amazon.com/cli/latest/reference/apigateway/update-stage.html) 命令更新访问日志格式以包含 `$context.customDomain.routingRuleIdMatched` 变量。此变量记录 API Gateway 用来向 API 发送流量的路由规则 ID。可以使用此日志来确认 API Gateway 是否使用路由规则将流量发送到 API。有关更多信息，请参阅 [我不知道 API Gateway 是如何将流量发送到 API 的](rest-api-routing-rules-troubleshoot.md#rest-api-routing-rules-logging)。

   ```
   aws apigateway update-stage \
     --rest-api-id a1bc2c3 \
     --stage-name prod \
     --patch-operations "op=replace,path=/accessLogSettings/format,value='\$context.path \$context.customDomain.routingRuleIdMatched \$context.requestId \$context.extendedRequestId'"
   ```

   如果您没有日志组，请参阅[为 API Gateway 中的 REST API 设置 CloudWatch 日志记录](set-up-logging.md)。

1. 使用以下示例 curl 命令来调用基本路径为 `orders/v2/items/categories/5` 的自定义域名。

   ```
   curl "https://api.example.com/orders/v2/items/categories/5
   ```

1. 使用以下 [filter-log-events](https://docs.aws.amazon.com/cli/latest/reference/logs/filter-log-events.html) 命令，来从日志组 `access-log-group-orders` 中获取包含路由规则 ID `abc123` 的日志事件。

   ```
   aws logs filter-log-events --log-group-name access-log-group-orders --filter-pattern abc123
   ```

    这确认 API Gateway 已使用路由规则向 API 发送流量。

------