3

I am taking over an AWS lambda serverless nodejs app and I am trying to figure out how to execute an endpoint in my development environment. For my purposes I am trying to simulate a request like the following:

http://localhost:3000/find/product?hiveProductId=22002233&zipCode=44035

the app includes the following in handler.js

app.route("/find/product").get(cors(), async (req, res) => {
    try {
        console.log(
            "finding product",
            req.query
        );

        etc...
    } catch (error) {
        console.error("caught error finding product: ", error);
        res.status(500).json(error);
    }
});

module.exports.find = serverless(app);

There is also the following in serverless.yml:

functions:
  find:
    handler: handler.find
    events:
      - http:
          path: /find/product
          method: GET
          memorySize: 128
          timeout: 30
          private: false
          cors: true
          integration: lambda-proxy
          request:
            parameters:
              querystrings:
                hiveProductId: true 
                max: false
                lat: false
                lon: false
                allowGeoIp: false
                zipCode: false
            methodReponses:
              - statusCode: "200"
                responseBody:
                description: "array of stores with product"
                responseModels:
                    "application/json": "Stores"
              - statusCode: "404"
                description: "not found"
      - http:
          path: /find/stores
          method: GET
          memorySize: 128
          timeout: 30
          integration: lambda-proxy
          private: true
          request:
            parameters:
              querystrings:
                max: false
                lat: true
                lon: true
          documentation:
            summary: "Find the closest stores"
            queryParams:
              - name: "lat"
                description: "latitude caller for geosearch"
                required: true
              - name: "lon"
                description: "longtitude caller for geosearch"
                required: true
              - name: "max"
                description: "maximum stores to location to return. default is 5"
            methodReponses:
              - statusCode: "200"
                responseBody:
                  description: "array of stores sorted by distance"
                responseModels:
                    "application/json": "Stores"

I have been using https://www.serverless.com/framework/docs/providers/aws/cli-reference/invoke-local/ for a reference. serverless invoke local seems like what I'm looking for. serverless invoke local --function find gives the following response:

{
    "statusCode": 404,
    "headers": {
        "x-powered-by": "Express",
        "content-security-policy": "default-src 'none'",
        "x-content-type-options": "nosniff",
        "content-type": "text/html; charset=utf-8",
        "content-length": "139"
    },
    "isBase64Encoded": false,
    "body": "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n<title>Error</title>\n</head>\n<body>\n<pre>Cannot GET /</pre>\n</body>\n</html>\n"
}

Any advice or pointers on how to use serverless invoke correctly, or a different approach to look into, or any documentation that would be more fruitful, would be greatly appreciated.

laertiades
  • 1,992
  • 2
  • 19
  • 26

2 Answers2

2

I haven't used this method before, so I can't say anything. I can recommend serverless-offline as a different solution. serverless-offline

0

You are asking how to test and debug serverless apps.. Here is the .vscode/launch.json(placed in root directory of VS Code project) I am using for it(I am using VS code always):

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Launch Lambda",
      "runtimeExecutable": "/Users/ninjablade/.nvm/versions/node/v12.18.0/bin/node",
      "cwd": "${workspaceFolder}/lambda-mock",
      "program": "/usr/local/bin/sls",
      "args": [
        "invoke",
        "local",
        "-f",
        "resolvers",
        "-p",
        "../mocks/resolvers.json"
      ],
      "skipFiles": ["<node_internals>/**"]
    },
  ]
}

As you can notice here, we are running $ sls invoke local -f function -p data command (you can check out exact guide for this command from sls framework documentation)

As a -p option of this command, we can pass any info we need for the endpoint. You can get the test event data from Cloud Watch log easily and replace it with your test input data.

$ sls invoke local command will simulate the exactly same lambda environment in your local development environment and let you test your lambda function in your local.

This post will help your a lot Debug Serverless app by VS Code. Thanks

harley
  • 396
  • 1
  • 8