简体   繁体   English

如何根据 AWS API Gateway 中的请求标头动态设置集成端点?

[英]How to set integration endpoint dynamically based on request header in AWS API Gateway?

I would like to proxy the incoming requests to different endpoints based on a request header received in the request.我想根据请求中收到的请求标头将传入请求代理到不同的端点。

In AWS API gateway, I can set up different endpoints as separate stage variables but at integration >> Endpoint URL setting, I would like to pick the stage variable based on the value of request header value.在 AWS API 网关中,我可以将不同的端点设置为单独的阶段变量,但是在集成 >> 端点 URL 设置中,我想根据请求标头值的值来选择阶段变量。

For example:例如:

if header value is brand-id: abc then request should be proxied to abc.test.com如果标头值为brand-id:abc 那么请求应该被代理到abc.test.com

if header value is brand-id: pqr then request should be proxied to pqr.test.com如果标头值为brand-id: pqr 那么请求应该被代理到pqr.test.com

I'm expecting something like this in "Endpoint URL" value: http://${stageVariables.${method.request.header.brand-id}}/我期待“端点 URL”值中的类似内容: http://${stageVariables.${method.request.header.brand-id}}/

Any help to achieve this would be appreciated.任何帮助实现这一目标将不胜感激。

AFAIK this is not possible on the API Gateway level. AFAIK 这在 API 网关级别是不可能的。 Option is to do the mapping on the lambda integration level.选项是在 lambda 集成级别上进行映射。

You can use Lambda Proxy Integration to achieve the similar behavior:您可以使用 Lambda 代理集成来实现类似的行为:

  • Create your required set of API's.创建所需的 API 集。
  • Create a proxy endpoint that will pass everything to the Lambda Function.创建一个代理端点,将所有内容传递给 Lambda 函数。
  • Inside the Lambda Function decide on the basis of headers to call the respective endpoints and pass the required data from the payload you got.在 Lambda 函数内部,根据标头决定调用相应的端点并从您获得的有效负载中传递所需的数据。
  • Return the response as it is from the API you called.返回来自您调用的 API 的响应。

You can use python's adapter pattern, or string parameter formatting to save yourself from the clutter of if and else conditions.您可以使用 python 的适配器模式或字符串参数格式来避免 if 和 else 条件的混乱。 You can also consider calling Lambdas directly from your proxy Lambda with RequestResponse invoke, that may save yourself some time caused by the extra layer of API Gateway.您也可以考虑通过RequestResponse调用直接从您的代理 Lambda 调用 Lambda,这可能会为您节省一些时间,因为 API 网关的额外层。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Cors - 如何处理需要自定义标头的预检选项请求? (AWS:使用 vpc 端点的私有 API 网关) - Cors - how to handle preflight options request that requires a custom header? (AWS: private API gateway using a vpc endpoint) AWS API Gateway-遇到端点后如何动态设置阶段变量? - AWS API Gateway - How can I dynamically set a stage variable after hitting an endpoint? 如何在AWS API Gateway中生成Set-Cookie集成响应头? - How can I generate a Set-Cookie integration response header in AWS API Gateway? Aws Api网关集成请求如何附加属性以请求正文? - Aws Api Gateway Integration Request How to append a property to request body? AWS Api Gateway 模拟集成响应标头映射,如何? - AWS Api Gateway mock integration response header mapping, how? 如何使用 Ansible 将 AWS API 网关集成请求正确设置为 lambda 函数? - How to set AWS API gateway integration request to lambda function correctly using Ansible? 如何在AWS API Gateway中基于IAM用户映射终端节点? - How to map endpoint based on IAM user in AWS API Gateway? 如何绕过 header 从请求进入 header 以响应 AWS API 网关? - How to bypass header from request into header in response in AWS API Gateway? 如何在现有 AWS API 网关集成请求中添加标头 - How to add headers in existing AWS API gateway integration request 如何在AWS API Gateway中以请求标头响应 - How do I respond with a request header in AWS API Gateway
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM