How to route traffic from one domain to another with AWS API Gateway

John Olatubosun
CodeX
Published in
4 min readJul 22, 2024

AWS API Gateway-another one of the myriad services on AWS đŸ€•. Well, every day is an opportunity to learn something new.

Introduction

AWS API Gateway is primarily used to “create, publish, maintain, monitor, and secure APIs at any scale”. It allows you to set up APIs easily and handles most of the implementation details (concurrency, access control, CORS and much more)

This service uses a pay-as-you-go pricing model. You pay for API calls and the amount of data transferred out. As with all AWS services be careful with implementation and how it would impact usage, so you don’t get hit by the widely feared AWS bill.

Here are some of the use cases of AWS API Gateway

  • Creating a REST API from a data source: API Gateway can be used to make REST endpoints available that fetch, add or manipulate data in a data source (e.g. DynamoDB)
  • Building Serverless Applications: It can be integrated to route requests to AWS lambda, allowing you to build applications without servers
  • Building Real-time Applications: API Gateway supports WebSocket APIs, facilitating real-time, two-way communication between clients and servers.
  • API Gateway as a Proxy: It can be used to proxy requests to other HTTP APIs, AWS Services or legacy applications without API functionality

In this article, I am focusing on using AWS API Gateway as a Proxy, for forwarding requests from one API to another

https://api.main.com → https://api.main-forwarded.com

Step 1 — Setting up the Gateway

Go to your AWS dashboard search for “API Gateway”, then on the service page click on “Create”, and select an “HTTP API”

Then give it a name of your choice. You can skip the optional stage for now, later on, we will cover that.

Step 2—Setting up forwarding rules

Go to “Routes” under “Develop”.

There you will create a new route, and use “ANY” and “/{proxy+}”. This would allow the HTTP API to catch all route paths

Next, go to “Integrations” under “Develop”

There you would find the route that you just created. Select the route that you created and click on “Create Integration”

Select “HTTP URI” as the integration target. or the integration detail put in “api.main-forwarded.com/{proxy}”. “{proxy}”, is added so it can properly route to the correct path if you previously used “/{proxy+}” when creating the route

Step 3 — Adding Custom Headers (Optional)

To add custom headers or edit existing headers that can done in the manage integrations tab, by creating a parameter mapping

The mapping type can be either incoming requests or outgoing responses depending on what you want to achieve

Step 4—Setting up the entry point

Your domain should already be on AWS Route 53 for easy integration

Go to custom domains there you will add a new domain. Select “Regional” as the endpoint type and attach an SSL certificate from AWS ACM

Once it is created, select the domain and create a new API mapping

Here you would map it to the API you created previously

Step 5— Configuring AWS Route 53

Firstly take note of your “API Gateway domain name”

Then go to AWS Route 53 and create a record that maps “https://api.main.com” to your domain name generated on API Gateway

Conclusion

And that's it 😄
 Congratulations on successfully using AWS API Gateway to route traffic from one API to another đŸ˜ŽđŸ€

If you have any questions or want to collaborate, reach out to me here

Stay coolđŸ«Ą

--

--

John Olatubosun
CodeX
Writer for

A man on a mission. I write about Christ, life and Tech. Reach out to me https://toluolatubosun.com/contact