A Cloud Architect Company
Amazon Web Services

How to route traffic to two API gateways based on path

Introduction

In this article, we are going to cover an API gateway service – API Gateway is a server that acts as an intermediary or ‘gateway’ for requests from clients seeking access to services, typically in a microservices architecture. It is a critical component that sits between external clients and internal services, providing various functionalities to manage, secure, and route traffic efficiently.

It plays a pivotal role in enabling scalable and maintainable microservices architectures by serving as the single point of entry for all client interactions with backend services.

If you would like to follow a video tutorial please use our youtube video.

Prerequisites

  • AWS account.
  • AWS account with Amazon API Gateway, Route53, and ACM Full Access, IAM user with full permissions.

Request ACM certificate

Go to ACM service ,select > request certificate and choose > request a public certificate and click > next

Type > your domain name, choose > DNS validation, and choose RSA 2048, and click > request, It takes a few minutes to validate the certificate.

ACM Certificate

 

ACM Certificate

Now the certificate was issued.

 

ACM Certificate

Create API gateway

We want to create two API gateway > go to API gateway service.

I am choosing rest API to build

API Gateway

 

Choose >> new APi, type >> API name, and click >> create API

API Gateway

Now we created the first API gateway. now we want to create another API gateway. do the same process again.

API Gateway

 

Now we successfully created two API gateways.

Create Lambda function

Now we need to create two lambda functions one is for API gateway one, and another one is for API gateway 2

Go to lambda service > create function > type a name, I am giving “skapi1” and I am choosing node js and click create function.

Lambda

I am changing some content in the message, click > deploy and type > event name, click > save

lambda function

Do the same process and create another lambda function.

Now we created two lambda functions, I am given the names “skapi1” and “skapi2

lambda function

Create custom domain name

Go to  API gateway service, click custom domain names, and click >> create

Custom domain name

Type your >> domain name

domain name

Choose your issued ACM certificate, and click >> create domain name

ACM certificate

After creating the domain name it assigns an endpoint, copy the endpoint

Endpoint

Create a record in route 53

Go to route 53 service, click >> create record, type >> your domain name, choose >> CNAME in the record type, paste the copied endpoint in the value, and click >> create records

Route 53

Integrate lambda with API gateways

Go to API gateway service, click your >> first API gateway

API gateway

Click >> create method

API gateway

In the method type choose >> GET, and choose >> lambda in the integration type

API gateway

In the lambda function choose your >> first created lambda function (skapi1), and click >> create method

api gateway

Now we integrated the lambda function in the first API gateway, click >> deploy API

Deploy api

In stage, click >> new stage, give a >> stage name, and click deploy

Deploy api

Now we successfully deployed, copy the invoke URL, and paste it to check

Api gateway

It shows the message that we created in the lambda function

lambda function

Now we successfully integrated the lambda function into the first API gateway, now we want to do the same process for the second API gateway.

but choose the lambda function that we created second (skapi2)

Api gateway

After doing all the setups, copy the invoke URL and paste it to check.

It shows the message that we created in the second lambda function

domain name

Configure api mappings

Click >> custom domain names, choose >> API mappings, click >> configure API mappings

API mappings

Click >> add  new mapping

API mappings

In api >> choose API gateway1, in stage >> choose your created stage

API mappings

Now click again >> add new mapping

In api >> choose api gateway 2, in stage >> choose your created stage, in path >> type any name (I am giving sub), and click save

API mappings

Now we successfully mapped the path, copy the domain and paste it into a new tab.

It will display the first api gateway content that we integrated into the first lambda function

Domain name

Now type “/sub” in the domain name, and it will display the second api gateway content that we integrated in the second lambda function.

Domain name

routing traffic to two API gateways based on the path can be efficiently managed using path-based routing rules within your API management platform or reverse proxy.

This enhances modularity, scalability, and independent management of APIs, facilitating easier updates and maintenance, and improving overall system performance.

Leave a Reply

Your email address will not be published. Required fields are marked *

back to top
advanced-floating-content-close-btn

Contact Us to save your AWS bill by 40%

X