Define optional query parameters in serverless.yml

Viewed 2220

I am fairly new to serverless framework (sls) and trying to define a function and deploy it to AWS.

While declaring a function in serverless.yml, I have defined the necessary parameters for a function along the lines of the following snippet but I am not able to define an optional query parameter in the http path of the new function:

function-name:
    handler: path/to/js/file.handler
    events:
      - http:
          path: api/v1/resource/{limit}
          method: GET

Desired behavior: The issue is that I am not able to declare an optional parameter in the path. I wish the endpoint would work as expected even if the limit parameter in not passed in the http GET request.

Note: I have tried path: api/v1/resource/{limit?} but no luck.

1 Answers

You can add another path for your Lambda to respond to.

function-name:
    handler: path/to/js/file.handler
    events:
      - http:
          path: api/v1/resource/{limit}
          method: GET
      - http:
          path: api/v1/resource/
          method: GET

Then, handle it properly inside your Lambda function.

Related