Skip to content

How to configure AWS API Gateway as a passthrough as well as sub domain rewriting

0

I have REST api. I am trying to configure AWS API Gateway as a passthrough. My api endpoint domain has subdomain part, which indicates tenant identity. Ex. customername.domain.com/api. My API Gateway url is xyz.com/api. In this case, all the requests from all the customers will come to xyz.com/api. However, I need to redirect the request to specific customer api endpoint based on on of the headers. Ex. header name is 'x-cust-domain'. A value in this header would be customerName, which is nothing but the subdomain part of the endpoint url. Ex. customername.xyz.com/api. So an api call comes to api gateway and the api gateway redirects it to the right customer endpoint based on the header value.

I tried authorizer lambda function but it did not work because it doesn't access to the domain portion of the request url. Also, it cannot overwrite the domain.

Also, I tried request redirection by implementing lambda function as an intermediary. In this case, the request comes to the lambda function where it responds with 302 and give the destination location as the customer api endpoint url. Somehow, I couldn't get this working.

Any help with this would be greatly appreciated. Thank you in advance!

1 Answer
0

To configure AWS API Gateway as a passthrough with subdomain rewriting based on a custom header, you can use a combination of API Gateway features and a custom integration. Here's an approach to achieve this:

  1. Set up your API Gateway as a proxy integration. This allows you to pass through the entire request to your backend service.

  2. In the API Gateway console, create a new resource with a greedy path variable (e.g., /{proxy+}) to catch all incoming requests.

  3. Set up an HTTP integration for this resource. Instead of pointing directly to your backend, you'll use a custom domain that includes a placeholder for the customer subdomain.

  4. In the Integration Request settings, you can use API Gateway's mapping templates to dynamically set the endpoint URL based on the custom header. Here's how you can do this:

    • In the Integration Request, add a mapping template for the Content-Type "application/json".

    • In the template, use the following VTL (Velocity Template Language) code:

      #set($customerName = $input.params('x-cust-domain'))
      #if($customerName == '')
        #set($customerName = 'default')
      #end
      {
        "endpoint": "https://$customerName.domain.com$context.path"
      }
      

    This code extracts the 'x-cust-domain' header value and uses it to construct the backend URL.

  5. In the Integration Response, you can use another mapping template to ensure the response from your backend is properly passed back to the client.

  6. Deploy your API to make the changes effective.

With this setup, when a request comes to your API Gateway (xyz.com/api), it will:

  1. Extract the 'x-cust-domain' header value
  2. Use this value to construct the appropriate backend URL (e.g., customername.domain.com/api)
  3. Forward the request to this dynamically constructed URL
  4. Return the response from the backend to the client

This approach allows you to achieve the subdomain rewriting based on the header value without needing to use a Lambda authorizer or implement redirection. It leverages API Gateway's built-in request transformation capabilities to dynamically route requests to the appropriate backend endpoint.

Remember to secure your API appropriately, possibly using API keys or other authentication methods, to ensure that the 'x-cust-domain' header can't be manipulated by unauthorized users to access other customers' endpoints.
Sources
how to pass the original url from API Gateway to the backend service | AWS re:Post
Module 4. Deploy a RESTful API

answered 2 years ago

AWS
SUPPORT ENGINEER

reviewed 2 years ago

  • can you please elaborate step #3 and #5.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.