How to Use a Single AWS API Gateway for Multiple AWS Lambda Functions

0

I have multiple services, each adhering to the formats api/users and api/products.

My goal is to establish paths like service1/api/users and service2/api/products in a Single AWS API Gateway, directing requests to the respective Lambda functions.

Specifically, in Service1, the endpoint is structured as api/users without a service name prefix (Service1/api/users). How can I set up the AWS API Gateway to effectively identify and route requests to this specific path within Service1? Furthermore, I aim to replicate this configuration for other services

Enter image description here

Enter image description here

2 Answers
0
Accepted Answer

Hello.

As far as I can see from the image, it looks like the creation of the resource has already been completed.
In the next step, just connect Lambda with each resource.
You can link Lambda by opening each resource and creating a method.
https://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started-rest-new-console.html#getting-started-rest-new-console-create-integration

profile picture
EXPERT
answered 5 months ago
  • Thank you for your reply. Do you have Lambda proxy integration enabled? I tried the same settings in my environment, but no 404 error occurred. a

  • Hi Yes, I did but it didn't work. I found the problem is Lambda Proxy forwarded requests within an incorrect resource path. My lambda function expected the path api/users but Lambda Proxy forwarded service1/api/users. That mean, they forwarded service1/{proxy+} instead of {proxy+} only.

    So the question is how can I custom the Resource Path to forward for Lambda Function? Or Do we have other ways to solve it?

  • I don't think it's wrong for the path to be "service1/api/users" because the API under "service1" is being executed. For example, how about trying to redirect on the Lambda side as shown below?

    def lambda_handler(event, context):
        return {
            "statusCode": 303,
            "headers": {
                "Location": "https://yyyyy.execute-api.ap-northeast-1.amazonaws.com/api/users"
            }
        }
    
0

Hi @Riku_Kobayashi,

Example:

When a user sends a request to the API Gateway with the path GET service1/api/users

The expected behavior is that the service should receive the request as GET api/users. Enter image description here

However, in reality, the actual path received is GET service1/api/users , error 404 Not Found.

Enter image description here Enter image description here

How can I set up the AWS API Gateway to effectively identify and route requests to this specific path within Service1

I suspect it's related to URL rewrite or mapping template. Enter image description here

answered 5 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions