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?

Emad Dehnavi
  • 3,262
  • 4
  • 19
  • 44
  • 1
    Agree with Denis.. you could, but why would you want to? Separate lambdas helps segregation and delegating responsibility between your lambdas. – James Jun 16 '18 at 03: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 : https://API_GETWAY_DOMAIN/STAGE/LAMBDA_FUNCTION is there a way to add something like route to it, like https://API_GETWAY_DOMAIN/STAGE/LOGIN/LAMBDA_FUNCTION – Emad Dehnavi Jun 16 '18 at 03: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? – Emad Dehnavi Jun 16 '18 at 03:57
  • 1
    How you architect your lambdas and your routes is completely decoupled. – James Jun 16 '18 at 04:01
  • @James the routes, can we define them in API Getway? then maybe we can detect it through lambda event inside the function... – Emad Dehnavi Jun 16 '18 at 04:07
  • 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. – James Jun 16 '18 at 04:58

1 Answers1

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.
Tom
  • 1,660
  • 8
  • 16