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

2.5k views Asked by At

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.

2

There are 2 answers

1
amsh On

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.

1
Milan Gatyás On

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