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

3 minute read
0

I want to use the host header value for my Amazon API Gateway endpoint or custom domain. I want to 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. Depending on the integration request endpoint URL, Amazon API Gateway assigns the host value.

To use the host header value of an API Gateway endpoint in the backend, use the custom header. 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, then a page that introduces you to the service appears. In REST API, choose Build. When the Create Example API pop-up window 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, enter these values:
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. 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. 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, then add a rule to test your configuration. Choose "http header" as a condition, and then 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.

Related information

Set up request and response data mappings using the API Gateway console

Set up an API using the API Gateway console

Amazon API Gateway important notes

How do I integrate an API Gateway REST API with an Application Load Balancer?

AWS OFFICIAL
AWS OFFICIALUpdated 9 months ago
4 Comments

Is there an example of this being created via IaC either Cloudformation or Terraform?

Patrick
replied 10 months ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

profile pictureAWS
MODERATOR
replied 10 months ago

I followed the above steps, I am using custom domain but still, the host header is the aws api gateway. Any suggestions or changes made?

replied 5 months ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

profile pictureAWS
MODERATOR
replied 5 months ago