aws-samples / aws-serverless-samfarm

This repo is full CI/CD Serverless example which was used in the What's New with AWS Lambda presentation at Re:Invent 2016.
Apache License 2.0
284 stars 240 forks source link

1 lambda function with several events (several http methods for restful api) #11

Open gavetisyanca opened 4 years ago

gavetisyanca commented 4 years ago

I have a different question. How do I have 1 lambda function with different events (different http methods) I don't want to create several lambda functions for each method its not interesting and not efficient. I want to be able trigger that function with either with boolean logic inside the actual script or even better edit this serverless.yml file so I can do that from yaml rather than inside the script Please help me, will much appreciate that

Originally posted by @gavetisyanca in https://github.com/aws-samples/aws-serverless-samfarm/issues/5#issuecomment-671002101

estebansolo commented 4 years ago

IMG_20200809_004804.jpg

You can add multiple events to a function, in this example, sqs and schedule, you just add what you need.

gavetisyanca commented 4 years ago

Thanks for your quick response , Much appreciated really @estebansolo I just don't know how to integrate this code with what you just posted : ) Can we NOT use SQS ? This code is from https://github.com/serverless/examples/tree/master/aws-node-rest-api-with-dynamodb

functions: create: handler: todos/create.create events:

estebansolo commented 4 years ago

In my case I haven't used serverless framework, however as I saw it could be as follows, just organize which file and handler is going to receive the requests and try to execute it.

https://www.serverless.com/framework/docs/providers/aws/events/schedule/

Here they use multiple events, although they are schedule type I would think it works the same way for http.

functions:
  function_name:
    handler: js_file.handler
    events:
      - http:
          path: todos
          method: post
          cors: true
      - http:
          path: todos
          method: get
          cors: true
      - http:
          path: todos/{id}
          method: get
          cors: true
      - http:
          path: todos/{id}
          method: put
          cors: true
      - http:
          path: todos/{id}
          method: delete
          cors: true
gavetisyanca commented 4 years ago

@estebansolo Bunch of thanks for your time and help