- Newest
- Most votes
- Most comments
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:
-
Set up your API Gateway as a proxy integration. This allows you to pass through the entire request to your backend service.
-
In the API Gateway console, create a new resource with a greedy path variable (e.g., /{proxy+}) to catch all incoming requests.
-
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.
-
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.
-
-
In the Integration Response, you can use another mapping template to ensure the response from your backend is properly passed back to the client.
-
Deploy your API to make the changes effective.
With this setup, when a request comes to your API Gateway (xyz.com/api), it will:
- Extract the 'x-cust-domain' header value
- Use this value to construct the appropriate backend URL (e.g., customername.domain.com/api)
- Forward the request to this dynamically constructed URL
- 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
Relevant content
asked 4 years ago
asked 3 years ago

can you please elaborate step #3 and #5.