如何使用路由规则 - Amazon API Gateway

如何使用路由规则

可以使用 AWS Management Console、AWS CLI 或任何 AWS SDK 创建路由规则。创建规则后,可以更改其优先级。

创建路由规则

以下过程说明如何在路由模式设置为 ROUTING_RULE_THEN_API_MAPPINGROUTING_RULE_ONLY 的情况下,为自定义域名创建路由规则。

AWS Management Console
  1. 通过以下网址登录到 Amazon API Gateway 控制台:https://console.aws.amazon.com/apigateway

  2. 从主导航窗格中选择自定义域名

  3. 选择自定义域名。

  4. 路由详情选项卡上,选择添加路由规则

  5. 选择添加新条件以添加新条件。

    您可以添加标头或基本路径条件。要将所有传入的请求与自定义域名匹配,请不要添加条件。

  6. 对于操作,使用下拉列表来选择目标 API 和目标阶段。

  7. 选择下一步

  8. 在优先级字段中,为优先级输入一个数字。

    API Gateway 按优先级顺序(从最低值到最高值)评估规则。

    如果您创建的规则没有条件,我们建议您使用高值优先级。

  9. 选择创建路由规则

AWS CLI

以下 create-routing-rule 命令创建优先级为 50 的路由规则。在此示例中,API Gateway 将任何具有标头 Hello:Worldx-version:beta 以及基本路径 PetStoreShopper 的传入请求路由到目标 API a1b2c3

aws apigatewayv2 create-routing-rule \ --domain-name 'api.example.com' \ --priority 50 \ --conditions '[ { "MatchHeaders": { "AnyOf": [ { "Header": "Hello", "ValueGlob": "World" } ] } }, { "MatchHeaders": { "AnyOf": [ { "Header": "x-version", "ValueGlob": "beta" } ] } }, { "MatchBasePaths": { "AnyOf": [ "PetStoreShopper" ] } } ]'\ --actions '[ { "InvokeApi": { "ApiId": "a1b2c3", "Stage": "prod" } } ]'

输出将与以下内容类似:

{ "Actions": [ { "InvokeApi": { "ApiId": "a1b2c3", "Stage": "prod", "StripBasePath": false } } ], "Conditions": [ { "MatchHeaders": { "AnyOf": [ { "Header": "Hello", "ValueGlob": "World" } ] } }, { "MatchHeaders": { "AnyOf": [ { "Header": "x-version", "ValueGlob": "beta" } ] } }, { "MatchBasePaths": { "AnyOf": [ "PetStoreShopper" ] } } ], "Priority": 50, "RoutingRuleArn": "arn:aws:apigateway:us-west-2:111122223333:/domainnames/api.example.com/routingrules/abc123", "RoutingRuleId": "abc123" }

更改路由规则的优先级

您可以更改路由规则的优先级。这将立即生效,并可能影响 API 使用者调用自定义域名的方式。我们建议您在设置路由规则的优先级时,在规则之间留出间隔。

例如,假设两个路由规则,规则 abc123 的优先级为 50,规则 zzz000 的优先级为 150。要更改规则的优先级,以便 API Gateway 首先评估 zzz000 规则,您可以将规则 zzz000 的优先级更改为 30。

AWS Management Console
  1. 通过以下网址登录到 Amazon API Gateway 控制台:https://console.aws.amazon.com/apigateway

  2. 从主导航窗格中选择自定义域名

  3. 选择自定义域名。

  4. 路由详情选项卡上,选择路由规则,然后选择编辑

  5. 选择下一步

  6. 对于优先级,请输入新的优先级。

  7. 选择保存更改

AWS CLI

以下 put-routing-rule 命令更改路由规则 abc123 的优先级。

aws apigatewayv2 put-routing-rule \ --domain-name 'api.example.com' \ --priority 30 \ --routing-rule-id abc123 \ --conditions '[ { "MatchHeaders": { "AnyOf": [ { "Header": "Hello", "ValueGlob": "World" } ] } }, { "MatchHeaders": { "AnyOf": [ { "Header": "x-version", "ValueGlob": "beta" } ] } }, { "MatchBasePaths": { "AnyOf": [ "PetStoreShopper" ] } } ]'\ --actions '[ { "InvokeApi": { "ApiId": "a1b2c3", "Stage": "prod" } } ]'

输出将与以下内容类似:

{ "Actions": [ { "InvokeApi": { "ApiId": "a1b2c3", "Stage": "prod", "StripBasePath": false } } ], "Conditions": [ { "MatchHeaders": { "AnyOf": [ { "Header": "Hello", "ValueGlob": "World" } ] } }, { "MatchHeaders": { "AnyOf": [ { "Header": "x-version", "ValueGlob": "beta" } ] } }, { "MatchBasePaths": { "AnyOf": [ "PetStoreShopper" ] } } ], "Priority": 38, "RoutingRuleArn": "arn:aws:apigateway:us-west-2:111122223333:/domainnames/api.example.com/routingrules/abc123", "RoutingRuleId": "abc123" }