1

I start study AWS serverless using lambda and API Getway, So I was thinking to create a REST API for a sample project. I noticed that in API Getway, we only can create the http methods which can trigger lambda functions, So I'm not sure if I get this right but, do we need one lambda function for each api route? or we can handle it somehow using one lambda function.

For example, lets say my project need api for below list :

  • Login
  • Register
  • GetUserData

Then if I going to make this by using API Getway and Lambda, I need to have 3 Lambda functions?

Like if I want create API for those mentioned above, it should be something like this for the endpoints ?

https://API_GETWAY_DOMAIN/STAGE/LAMBDA_FUNCTION_FOR_LOGIN
https://API_GETWAY_DOMAIN/STAGE/LAMBDA_FUNCTION_FOR_REGISTER
https://API_GETWAY_DOMAIN/STAGE/LAMBDA_FUNCTION_FOR_GET_USER_DATA

And is there any way to send multi http request to one lambda function, and handle each one of them inside the lambda?

6
  • 1
    Agree with Denis.. you could, but why would you want to? Separate lambdas helps segregation and delegating responsibility between your lambdas. Commented Jun 16, 2018 at 3:51
  • @DenisTsoi But how can I detect it, like this call is for login, or it's for register , etc. I mean the endpoint structure is like this : API_GETWAY_DOMAIN/STAGE/LAMBDA_FUNCTION is there a way to add something like route to it, like API_GETWAY_DOMAIN/STAGE/LOGIN/LAMBDA_FUNCTION Commented Jun 16, 2018 at 3:51
  • @James Maybe I get this wrong, I want look at this from routing point of view, so basically, each route, is a lambda function, right? Commented Jun 16, 2018 at 3:57
  • 1
    How you architect your lambdas and your routes is completely decoupled. Commented Jun 16, 2018 at 4:01
  • 1
    Maybe I don't understand what you're trying to do. You add resources then map particular resources to "actions" such as a lambda function. I wouldn't have two resources or paths mapped to the same function. It makes thing confusing and adds another layer of complexity in your app that API Gateway can do itself. Commented Jun 16, 2018 at 4:58

1 Answer 1

1

It is possible to have multiple API Gateway routes direct to the same Lambda function. How to set that up depends on how you're maintaining your infrastructure.

For example, if you're using CloudFormation with the Serverless Application Model (SAM), which I recommend since it's probably the most straightforward way to keep track of serverless infrastructure (and infrastructure as code = awesome), you would define your AWS::Serverless::Function with a separate API Gateway source event for each route you define in your AWS::Serverless::Api.

Something like the following:

YourApi:
  Type: AWS::Serverless::Api
  Properties:
    ...
    DefinitionBody:
      swagger: 2.0
      ...
      paths:
        '/Login':
          post:
            x-amazon-apigateway-integration:
              # APIG->Lambda requests are always POST
              httpMethod: post
              type: aws_proxy
              uri:
                Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${YourLambda.Arn}/invocations
        '/Register':
          post:
            x-amazon-apigateway-integration:
              # APIG->Lambda requests are always POST
              httpMethod: post
              type: aws_proxy
              uri:
                Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${YourLambda.Arn}/invocations
        '/GetUserData':
          get:
            x-amazon-apigateway-integration:
              # APIG->Lambda requests are always POST
              httpMethod: post
              type: aws_proxy
              uri:
                Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${YourLambda.Arn}/invocations

YourLambda:
  Type: AWS::Serverless::Function
  Properties:
    ...
    Events:
      login:
        Type: Api
        Properties:
          Path: '/Login'
          Method: post
          RestApiId: {Ref: YourApi}
      register:
        Type: Api
        Properties:
          Path: '/Register'
          Method: post
          RestApiId: {Ref: YourApi}
      getUserData:
        Type: Api
        Properties:
          Path: '/GetUserData'
          Method: get
          RestApiId: {Ref: YourApi}

Keep in mind, though, that there are pros and cons to consolidating routes into one Lambda function. This StackOverflow question/answer explores that, but I'd like to add a few more benefits to separated Lambda functions:

  • Clearer metrics on how often your routes are getting called - You get invocations/errors/etc. metrics in CloudWatch for each Lambda function out-of-the-box, so separating them can make it easy to see how often people are registering versus logging in, etc.
  • More granular alarming - You can set different latency/error/etc. thresholds for different routes, and if an alarm goes off, you know exactly which Lambda function it's for.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Tom, Your answer helped me to understand the whole process better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.