Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

serverless-offline optional path parameter

I'm trying to set up a GET request with an optional parameter but I get an error when I call the url locally without the optional parameter. It works fine online on lambda though. What did I do wrong?

I'm using serverless version 1.24.1 with the serverless-offline plugin version 3.16.0

here is my request definition in serverless.yml:

functions:
getitems:
    handler: lambda.handler
    events:
      - http:
            path: item/store/{storeid}/{itemstatus}
            method: get
            cors: true
            request:
                parameters:
                  paths:
                    storeid: true
                    itemstatus: false

this url works:

http://localhost:3000/item/store/123456/used

this don't

http://localhost:3000/item/store/123456

and gives me this output

{
   statusCode: 404,
   error: "Serverless-offline: route not found.",
   currentRoute: "get - /item/store/123456",
   existingRoutes: [
       "get - item/store/{storeid}/{itemstatus}"
   ]
}

Thanks a lot

like image 211
Tom Ines Avatar asked Nov 29 '17 16:11

Tom Ines


People also ask

How do I change serverless offline ports?

The workaround for this is to keep serverless-offline-local plugin enabled in only one service (if you have two or more). Example, In my-service-1 you keep all dynamodb config in serverless. yaml file and start this service with default port: sls offline start --migrate true .

How does serverless offline work?

This Serverless plugin emulates AWS λ and API Gateway on your local machine to speed up your development cycles. To do so, it starts an HTTP server that handles the request's lifecycle like APIG does and invokes your handlers.

How can I test serverless offline?

Since you already know the basics of Serverless Framework, you must have installed the framework. Next, install the package serverless-offline from npm. 2. Add this installed plugin to your serverless project.

How do I run serverless locally?

The serverless offline plugin for Node. js allows you to emulate AWS Lambda and API Gateway on a local machine. By using the serverless offline plugin, you can test your serverless applications without deploying them every time you make a change.


2 Answers

Unfortunately Chen Dachao's answer fails with:

An error occurred: ApiGatewayResourceExperimentExperimentVarPsizeVar - Resource's path part only allow a-zA-Z0-9._- and curly braces at the beginning and the end.

The current workaround to this is adding http handlers for each 'optional' variable in the path like so:

functions:
  getitems:
    handler: lambda.handler
      events:
        - http:
            path: item/store/{storeid}
            method: get
            cors: true
            request:
              parameter:
                storeid: true
        - http:
            path: item/store/{storeid}/{itemstaus}
            method: get
            cors: true
            request:
              parameter:
                storeid: true
                itemstatus: true
like image 109
CoderX Avatar answered Oct 21 '22 12:10

CoderX


Add "?" after the params can make it work.

functions:
  getitems:
  handler: lambda.handler
  events:
    - http:
        path: item/store/{storeid}/{itemstatus?}
        method: get
        cors: true
like image 22
Chen Dachao Avatar answered Oct 21 '22 11:10

Chen Dachao