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.
Now the certificate was issued.
Create API gateway
We want to create two API gateway > go to API gateway service.
I am choosing rest API to build
Choose >> new APi, type >> API name, and click >> create API
Now we created the first API gateway. now we want to create another API gateway. do the same process again.
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.
I am changing some content in the message, click > deploy and type > event name, click > save
Do the same process and create another lambda function.
Now we created two lambda functions, I am given the names “skapi1” and “skapi2”
Create custom domain name
Go to API gateway service, click custom domain names, and click >> create
Type your >> domain name
Choose your issued ACM certificate, and click >> create domain name
After creating the domain name it assigns an endpoint, copy the 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
Integrate lambda with API gateways
Go to API gateway service, click your >> first API gateway
Click >> create method
In the method type choose >> GET, and choose >> lambda in the integration type
In the lambda function choose your >> first created lambda function (skapi1), and click >> create method
Now we integrated the lambda function in the first API gateway, click >> deploy API
In stage, click >> new stage, give a >> stage name, and click deploy
Now we successfully deployed, copy the invoke URL, and paste it to check
It shows the message that we created in the 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)
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
Configure api mappings
Click >> custom domain names, choose >> API mappings, click >> configure API mappings
Click >> add new mapping
In api >> choose API gateway1, in stage >> choose your created stage
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
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
Now type “/sub” in the domain name, and it will display the second api gateway content that we integrated in the second lambda function.
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.