疑難排解路由規則的問題 - Amazon API Gateway

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

疑難排解路由規則的問題

下列疑難排解指引可能有助於解決路由規則的問題。

我無法得知 API Gateway 如何將流量傳送到我的 APIs

您可以使用 REST API 階段的存取日誌來記錄路由規則並進行疑難排解。您可以檢視 API Gateway 用來使用 $context.customDomain.routingRuleIdMatched變數將流量傳送至 API 的路由規則 ID。若要檢視 API Gateway 用來將流量傳送到 API 的 API 映射,請使用 $context.customDomain.basePathMatched變數。

若要記錄您的路由規則,您需要為您的帳戶設定適當的 CloudWatch Logs 角色 ARN,並建立日誌群組。

下列存取日誌群組範例可以擷取疑難排解路由規則和 API 映射的相關資訊。API Gateway 只會為所使用的路由機制填入內容變數,否則內容變數為 -

CLF
$context.path $context.customDomain.routingRuleIdMatched $context.customDomain.basePathMatched $context.requestId $context.extendedRequestId
JSON
{"requestPath": "$context.path", "routingRuleId" : "$context.customDomain.routingRuleIdMatched", "API mapping" : "$context.customDomain.basePathMatched", "requestId":"$context.requestId", "extendedRequestId":"$context.extendedRequestId"}
XML
<request id="$context.requestId"> <requestPath>$context.path</requestPath> <ruleId>$context.customDomain.routingRuleIdMatched</ruleId> <ApiMapping>$context.customDomain.basePathMatched</ApiMapping> <extendedRequestId>$context.extendedRequestId</extendedRequestId> </request>
CSV
$context.path,$context.customDomain.routingRuleIdMatched,$context.customDomain.basePathMatched,$context.requestId,$context.extendedRequestId

我們也建議您確認自訂網域名稱的路由模式。如需詳細資訊,請參閱為您的自訂網域名稱設定路由模式

我無法在自訂網域名稱上啟用路由規則

您可能會從 API Gateway 收到下列錯誤:

Your account doesn’t have permission to use RoutingRules. This might be caused by an IAM policy in your account with a deny statement on BasePathMapping or ApiMapping. To grant permission for this account to use RoutingRules, use the UpdateAccount API. This will impact any existing IAM policies that deny access to BasePathMapping or ApiMapping. See API Gateway documentation for further details.

如果 或 IAM 政策拒絕存取 BasePathMappingApiMapping,您將會收到此錯誤。當您啟用自訂網域名稱的路由規則時,雖然您的政策會繼續拒絕存取 BasePathMappingApiMapping,但相同的政策可用來存取 RoutingRule。這可讓使用者變更自訂網域名稱的路由行為。

例如,如果您有如下所示的政策:

{ "Sid": "DenyCreatingApiMappings", "Effect": "Deny", "Action": "apigateway:POST", "Resource": [ "arn:aws:apigateway:us-west-2::/domainnames/example.com/apimappings" ] }

當您啟用 的路由規則時example.com,此政策將繼續拒絕建立 的存取權,ApiMapping但不會拒絕建立 的存取權RoutingRule

我們建議您稽核帳戶中的 IAM 政策。下列範例政策會拒絕建立 ApiMappingBasePathMapping和 的存取權RoutingRule

{ "Sid": "DenyCreatingBasePathMappingsApiMappings", "Effect": "Deny", "Action": "apigateway:POST", "Resource": [ "arn:aws:apigateway:us-west-2::/domainnames/example.com/basepathmappings", "arn:aws:apigateway:us-west-2::/domainnames/example.com/apimappings" ] }, { "Sid": "DenyCreatingRoutingRules", "Effect": "Deny", "Action": "apigateway:CreateRoutingRule", "Resource": [ "arn:aws:apigateway:us-west-2:111122223333:/domainnames/example.com/routingrules/*" ] }

確認所有政策都已更新後,您可以更新 API 的帳戶層級設定,以啟用區域的路由規則。

使用下列 update-account 命令來更新 API 帳戶層級設定的設定:

aws update-account --patch-operations 'op=remove,path=/features,value=BlockedForRoutingRules'

更新 API 的帳戶層級設定後,您可以變更自訂網域名稱的路由模式。您也可以繼續使用 IAM 政策來拒絕存取 RoutingRulesApiMappingBasePathMapping