Skip to content
This repository has been archived by the owner on Apr 30, 2024. It is now read-only.

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

Open
gavetisyanca opened this issue Aug 9, 2020 · 4 comments

Comments

@gavetisyanca
Copy link

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 #5 (comment)

@estebansolo
Copy link

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
Copy link
Author

gavetisyanca commented Aug 9, 2020

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:
- http:
path: todos
method: post
cors: true

list:
handler: todos/list.list
events:
- http:
path: todos
method: get
cors: true

get:
handler: todos/get.get
events:
- http:
path: todos/{id}
method: get
cors: true

update:
handler: todos/update.update
events:
- http:
path: todos/{id}
method: put
cors: true

delete:
handler: todos/delete.delete
events:
- http:
path: todos/{id}
method: delete
cors: true

@estebansolo
Copy link

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
Copy link
Author

@estebansolo Bunch of thanks for your time and help

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants