5
votes

I'm trying to run locally a node lambda to debug it. I am using Serverless and this launch config in vsCode

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Launch Program",
      "program": "${workspaceRoot}/node_modules/.bin/sls",
      "args": [
        "invoke",
        "local",
        "--function",
        "hello",
        "--data",
        "hello world"
      ]
    }
  ]
}

My export.handler looks like this:

module.exports.handler = (event, context, callback) => {
  if (event.triggerSource === CONSTANTS.TRIGGER_SOURCE) {
    console.log("event = " + JSON.stringify(event));
    const uri = process.env.SCT_URL_BASE;
    const country = process.env.SCT_COUNTRY;

    const username = 
event.request.userAttributes[CONSTANTS.USER_ATTRIBUTES];
    const codeP = event.request.codeParameter;
    console.log("URI = " + url);
    console.log("Code:" + codeP);

    getUrlData(uri, country, username, codeP);
  } else {
    context.done(null, event);
  }
};

When I run de debug mode it does nothing. Serverless does not throw any error, I just can not reach inside the function.

Also, there is another thing I can not understand. In serverless documentation it said:

--function or -f The name of the function in your service that you want to invoke locally. Required.

I don't know what they are refering in this, if a function that we call to run the lambda or the function that it is called when the lambda is called. In this case, the function that I am exporting is "handler" but it doesn't work either.

Thanks in advance.

2
What caused this for me was running the sls command from outside of the Serverless service directory. Make sure you you're in the correct directory. - Harvinder

2 Answers

0
votes

I have used this approach and it works for me:

https://standardofnorms.wordpress.com/2017/12/03/locally-debugging-aws-lambdas-written-in-node-js/

The bad thing is that I would like to use serverless and not lambda-local package due to the greater community of serverless. Lambda-local works like charm though, so I send a big hug to its creator from here.

Answers to the first question are still very welcome.

EDIT: Ok, I figured this out.

Results that Serverless, as framework, uses a serverless.yml file when we need to add some configuration. There, I had to create the function I am going to run with the serverless command and then point it to the file where I have my handler. This is my serverles.yml right now:

service: serverless-simple

frameworkVersion: ">=1.1.0 <2.0.0"

provider:
  name: aws
  runtime: nodejs4.3

functions:
  lambdaHandler:
    handler: src/customMessageLambda.handler
    events:
      - http:
          path: ping

Sure I have to research a little more on this file but I have solved my issue.

Hope this helps someone, sometime.

0
votes

Running serverless on a server locally is both oxymoronic and annoying...

How about Using a debugging solution for live lambdas like Rookout ? It works on SAM local, but even better - it works on live functions in AWS!