How can I forward the host header with private integration for an API Gateway REST API?

Last updated: 2022-11-29

I want to use the host header value for my Amazon API Gateway endpoint or custom domain. How can I forward the host header with private integration for an API Gateway REST API?

Short description

Amazon API Gateway overwrites the host value that it receives in the original request to the integration endpoint. Amazon API Gateway assigns the host value depending on the integration request endpoint URL.

To use the host header value of an API Gateway endpoint in the backend, use the custom header. You can use the custom header in the integration request that contains the value of the request host header.

Resolution

Create an API Gateway REST API

1.    Open the API Gateway console.

2.    Choose Create API.

-or-

(If this is your first time using API Gateway) A page that introduces you to the features of the service appears. In REST API, choose Build. When the Create Example API popup appears, choose OK.

3.    For Choose an API type, in the REST API pane, choose Build.

4.    In Create new API, choose New API.

5.    In Settings, do the following:
For API name, enter a name that describes your API's purpose. For example: ForwardHostHeader.
(Optional) For Description, enter a short description of your API's purpose. for example, Forward host header for private integration.
For Endpoint Type, choose either Regional, Edge optimized, or Private.

6.    Choose Create API.

Configure your API's method request and Integration request

1.    Open the API Gateway console.

2.    Follow the instructions to create an API with private integration using the API Gateway console.

3.    In Method Request, add HTTP Request Headers as host.

4.    In Integration Request, expand HTTP Headers, and choose Add header.

5.    Enter a name for the custom header. For example, my_host.

6.    In Mapped from, enter the method request. For example, method.request.header.host, and then choose the check mark icon.

Deploy your API to a new stage

1.    Open the API Gateway console.

2.    Follow the instructions to Deploy a REST API to a stage. For more information, see Setting up a stage using the API Gateway console.

3.    In the Stage Editor pane, copy the Invoke URL to your clipboard. You can use this URL to invoke your API.

REST API invoke URL example:

https://1a2bc3d456.execute-api.us-east-1.amazonaws.com/stage

4.    If you use an Application Load Balancer for your backend, you can test this configuration by adding a rule. Choose "http header" as a condition and specify "my_host" as the same header and value as the API Gateway host value.

5.    Paste the API Gateway URL into a browser window to invoke your API. The "my_host" header configuration in API Gateway forwards the API Gateway host value to the Application Load Balancer. The Application Load Balancer matches the condition set and then returns the response as configured.