对路由规则相关问题进行故障排除
以下故障排除指南可能有助于解决与路由规则相关的问题。
我不知道 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.
如果 IAM 策略拒绝访问 BasePathMapping 或 ApiMapping,您将收到此错误。当您为自定义域名启用路由规则时,尽管策略将继续拒绝访问 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 update-account --patch-operations 'op=remove,path=/features,value=BlockedForRoutingRules'
更新 API 的账户级设置后,您可以更改自定义域名的路由模式。也可以继续使用 IAM 策略来拒绝访问 RoutingRules
、ApiMapping
或 BasePathMapping
。