Shared API Gateway between multiple services

Dilusha Gonagala
Jul 10 · 3 min read

In this article let’s look at how we can share One AWS API Gateway between multiple Services.

TechStack: AWS API Gateway, AWS Lambda, Serverless Framework, JavaScript

Prerequisites:

  • AWS installed and configured locally: Guide
  • Serverless Installed Globally: Guide

Assuming you have the above set up in your local machine, let’s begin.

Creating the Root API Gateway

Go ahead and create a separate directory of your choice and let’s call it, rest-api-root and then cd into the created directory and then create the below files,

First, create a simple handler for the main lambda as below,

Now create the below serverless.yml configuration

Now that we have the serverless configuration and the basic handler done for the Root API, let’s deploy this and get started.

Since you have serverless installed globally, you could run the below command to deploy this simple service.

sls deploy -v

Once the command is finished executing, it should have provisioned necessary resources under the hood and finally expose an API gateway endpoint URL, something similar like below,

https://{SOME-ID}.execute-api.us-east-1.amazonaws.com/dev/

If you access this URL, you will be able to see the output like below,

{"message": "Go Serverless!"}

Now that we have created the Root API Gateway with a simple service, let’s go ahead and create another Lambda Service which will share the same API Gateway provisioned above.

Create a child service that will share the above API Gateway

Create a separate directory outside the rest-api-root , call it lambda-service1.

Inside the lambda-service1 directory, create the below files.

Create the handler for this service,

Now before we create the serverless.yml config file, we would need to know two things,

1. API Gateway ID

2. API Gateway root resource ID

Two of the above can be found using AWS Console, once you login to the console, visit the AWS API Gateway

As you can see in the above screenshot, you would need to grab those two ids and put them in the below serverless configuration for your other Lambda Service.

Once this is also done, now we can deploy our second lambda service, which will use the same API Gateway that we have created earlier.

sls deploy -v

*Important: notice that, once this is deployed you will see that the generated endpoint output will contain /hello at the end of the URL, because we have defined a new method route for this lambda service.

events:
- http:
path: /hello

Now if you goto API Gateway on the Console, you will notice that it has created a new path on the same gateway to map the new lambda service like below,

You would be able to properly scale this solution to support multiple services which could use one Root API gateway. Which makes it easier and less complicated to manage.

Originally published at https://dilushagonagala.com on July 10, 2021.

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Dilusha Gonagala

Written by

In the Journey of Exploring the Universe...

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.