How to route traffic from one domain to another with AWS API Gateway
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đ«Ą