2

Is it possible to have separate handler for one function but each event type.

For example.

    Type: AWS::Serverless::Function
    Properties:
      FunctionName: ecommerce-lambda
      Handler: src/handlers/ecommerceHandler.handler.   <--- HERE is one handler for all event type below. 
      Runtime: nodejs12.x
      Description: Products Lambda function.
      Policies:
        - AWSLambdaBasicExecutionRole
        - AmazonDynamoDBFullAccess
      Environment:
        Variables:
          LAMBDA_ENVIRONMENT: local
      Events:
        # Products
        GetProductsAPI:    <---- Can this be some other handler?
          Type: Api
          Properties:
            Path: /api/products
            Method: GET
        PostProductsAPI:    <---- Can this be some other handler?
          Type: Api
          Properties:
            Path: /api/products
            Method: POST 

Given that I dont want to do following.

Resources:
  ApiResource:
    Type: AWS::Serverless::Api
    Properties:
      StageName: prod  
  
  TimeFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: firstsample/firstsample.handler   # firstsample.js file is in firstsample direcotory
      Role: !GetAtt BasicAWSLambdaRole.Arn
      Runtime: nodejs6.10
      CodeUri: ./                          # Look for code in the same directory as the output SAM file
      Events:
        MyTimeApi:
          Type: Api
          Properties:
            Path: /TimeResource
            Method: GET
            RestApiId: !Ref ApiResource
  
  SecondSampleFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: secondsample.handler                  # didn't have to include secondsample directory
      Role: !GetAtt BasicAWSLambdaRole.Arn
      Runtime: nodejs6.10
      CodeUri: ./secondsample           # code is in the secondsample directory, located in same directory
      Events:
        MyTimeApi:
          Type: Api
          Properties:
            Path: /TextResource
            Method: GET
            RestApiId: !Ref ApiResource

I want the setup where I want to each endpoint to be handle by its own lambda. GET should have its own lambda, POST should have its own lambda.

Surjit Singh
  • 269
  • 3
  • 8

1 Answers1

0

You can define two separated functions with different handlers with a common path, one with a GET method and the second with the POST method.

for example:

myGetFunction:
  Type: AWS::Serverless::Function
  Properties:
    Handler: src/handlers/get-all-items.getAllItemsHandler
    Description: A simple example includes a HTTP get method to get all items
    Events:
      Api:
        Type: Api
        Properties:
          Path: /
          Method: GET

myPostFunction:
  Type: AWS::Serverless::Function
  Properties:
    Handler: src/handlers/put-item.putItemHandler
    Description: A simple example includes a HTTP post method to add item
    Events:
      Api:
        Type: Api
        Properties:
          Path: /
          Method: POST
Guy
  • 1
  • 2