

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用路由規則重新建立 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. 在以下網址登入 API Gateway 主控台：[https://console.aws.amazon.com/apigateway](https://console.aws.amazon.com/apigateway)。

1. 從主要導覽窗格中，選擇 **Custom Domain Names (自訂網域名稱)**。

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. 在以下網址登入 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) 命令，從包含路由規則 ID `abc123` 的日誌群組 `access-log-group-orders` 取得日誌事件。

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

    這樣做可確認 API Gateway 使用路由規則將流量傳送至 API。

------