4

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.

For example:

if header value is brand-id: abc then request should be proxied to abc.test.com

if header value is brand-id: pqr then request should be proxied to pqr.test.com

I'm expecting something like this in "Endpoint URL" value: http://${stageVariables.${method.request.header.brand-id}}/

Any help to achieve this would be appreciated.

Satish Gadhave
  • 2,880
  • 3
  • 20
  • 27
  • 1
    Did you figure it out? I'm looking for something similar and also want to avoid Lambda. – diegosasw Mar 18 '21 at 09:01
  • 1
    There is no out-of-the-box support in API gateway. I had to do it using lambda proxy function. – Satish Gadhave Mar 19 '21 at 11:46
  • Maybe a bit late, but maybe a request parameter mapping will help. In the console go to integrations -> manage integration. Then create a parameter mapping with: - Parameter to modify: path - Modification type: overwrite - Value: something like `http://${stageVariables.${method.request.header.brand-id}}/` – Luc Hendriks Apr 30 '21 at 14:01
  • @SatishGadhave Could you please provide your solution or workaround if you can. Thank you. It will help for others who are trying to do the same. – Ravi MCA Aug 11 '21 at 19:17
  • 1
    @RaviMCA As I mentioned above, I used lambda proxy integration. I found nodeJS based proxy code. You can maintain mapping of endpoints in a separate file (module). Ref: https://stackoverflow.com/a/63602976/1197013 – Satish Gadhave Aug 13 '21 at 06:52

2 Answers2

0

AFAIK this is not possible on the API Gateway level. Option is to do the mapping on the lambda integration level.

Milan Gatyás
  • 2,509
  • 1
  • 17
  • 23
0

You can use Lambda Proxy Integration to achieve the similar behavior:

  • Create your required set of API's.
  • Create a proxy endpoint that will pass everything to the Lambda Function.
  • 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.
  • Return the response as it is from the API you called.

You can use python's adapter pattern, or string parameter formatting to save yourself from the clutter of if and else conditions. 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.

amsh
  • 3,097
  • 2
  • 12
  • 26