對路由規則的問題進行疑難排解
下列疑難排解指引可能有助於解決路由規則的問題。
我無法得知 API Gateway 如何將流量傳送至 API
您可以使用 REST API 階段的存取日誌來記錄路由規則並進行疑難排解。您可以使用 $context.customDomain.routingRuleIdMatched
變數來檢視 API Gateway 用來將流量傳送至 API 的路由規則 ID。若要檢視 API Gateway 用來將流量傳送至 API 的 API 映射,請使用 $context.customDomain.basePathMatched
變數。
若要記錄您的路由規則,您需要為您的帳戶設定適當的 CloudWatch Logs 角色 ARN,並建立日誌群組。
下列範例存取日誌群組可以擷取相關資訊,以對路由規則和 API 映射進行疑難排解。API Gateway 只會針對其使用的路由機制填入內容變數,否則內容變數為 -
。
我們也建議您確認自訂網域名稱的路由模式。如需更多詳細資訊,請參閱 為您的自訂網域名稱設定路由模式。
我無法在自訂網域名稱上啟用路由規則
您可能會從 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.
如果目前或過去有拒絕存取 BasePathMapping 或 ApiMapping 的 IAM 政策,則會收到此錯誤。當您針對自訂網域名稱啟用路由規則時,雖然您的政策會繼續拒絕對 BasePathMapping
或 ApiMapping
的存取,但可使用相同的政策存取 RoutingRule
。如此就可讓使用者變更自訂網域名稱的路由行為。
例如,如果您的政策如下:
{ "Sid": "DenyCreatingApiMappings", "Effect": "Deny", "Action": "apigateway:POST", "Resource": [ "arn:aws:apigateway:us-west-2::/domainnames/example.com/apimappings" ] }
當您針對 example.com
啟用路由規則時,此政策將繼續拒絕建立 ApiMapping
的存取,但不會拒絕建立 RoutingRule
的存取。
我們建議您稽核帳戶中的 IAM 政策。下列範例政策將會拒絕建立 ApiMapping
、BasePathMapping
和 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 apigateway update-account --patch-operations 'op=remove,path=/features,value=BlockedForRoutingRules' --region
us-west-2
更新 API 的帳戶層級設定後,您可以變更自訂網域名稱的路由模式。您也可以繼續使用 IAM 政策來拒絕對 RoutingRules
、ApiMapping
或 BasePathMapping
的存取。