Is it possible to allow multiple request methods e.g. GET
& PUT
for the same httpApi route?
The only way I can see to do it as by passing a wildcard *
as the method and then doing the method validation on the lambda itself.
Is it possible to allow multiple request methods e.g. GET
& PUT
for the same httpApi route?
The only way I can see to do it as by passing a wildcard *
as the method and then doing the method validation on the lambda itself.
Of course. You can define multiple functions with the same path but different method. Take a look at the docs. This is pretty simple.
I was wanting to use the same lambda function.
But it looks like I can declare another function in serverless.yml and point to the same function code.
Just a shame you cant have multiple http events with the same path but different methods under the same function declaration
I see what you mean. Yes you need multiple function definitions pointing to the same file. However splitting up the functions into different files is a best practice in my opinion. I don’t know why you would want to handle several different cases in the same file. Keep the single responsibility principle in mind. Any common functionality you ca put in other files.
You certainly can.
Just whipped up a quickie poc of it with:
...
"input": {
"version": "2.0",
"routeKey": "GET /hello",
"rawPath": "/hello",
...
AND
$ mkdir multiroute
$ cd multiroute
$ sls create -t aws-nodejs
Then edit the serverless.yml to look like:
service: multiroute
frameworkVersion: '2'
provider:
name: aws
runtime: nodejs12.x
lambdaHashingVersion: 20201221
stage: dev
region: us-west-2
functions:
hello:
handler: handler.hello
events:
- httpApi:
path: /hello
method: get
- httpApi:
path: /hello
method: post
Didn’t even touch the generated handler.js file.
Deployed via
sls deploy
Then hit it with Postman with both a GET and a POST
and got back the extected response:
...
"input": {
"version": "2.0",
"routeKey": "POST /hello",
"rawPath": "/hello",
...
Hope it helps.
I tried this and it did not work for me. I am using frameworkVersion 3, I wonder if that makes a difference.
hello, I configured the multiple path as you described above . But, its throwing error while deployment in AWS. I have used " - serverless-plugin-split-stacks " in my project.