Skip to Content
Technical Articles
Author's profile photo Carlos Roggan

Writing Function-as-a-Service [4]: Debugging

This blog is part of a series of tutorials explaining how to write serverless functions using the Functions-as-a-Service offering in SAP Cloud Platform Serverless Runtime

Quicklinks:
Debug Config
Quick Guide

This is again an amazing blog post.
Not because the blog itself, nor the writer would be so amazing:
It is the fact that it is possible to debug serverless functions
Which is – in conjunction with local development shown in the previous blog – a fantastic feature of Function-as-a-Service in Serverless Runtime.

Note:
This is a small tutorial, but explained in detail for beginners.
Advanced users can directly jump to the Quick Guide

Perequisites

You must have followed the previous blog, showing how to write and run functions on your local laptop.
As such, I assume you have the FaaS SDK installed on your local machine

You need an IDE which supports debugging, e.g. Visual Studio Code, which I’m using for this tutorial

Finally you don’t even need to be connected to the internet.
Really, you can switch off your internet connection
(But don’t delete the internet: you’ll need it to press “like” afterwards)

Preparation

Since we want to debug a function, we need a function project.
If you don’t have anything at hand, you can create a project like this

 

Configure IDE

Open Project

In Visual Studio Code, open the “File” menu, then click “Open Folder” and enter the project root folder of your functions project

Create Debug Configuration

Now that we’ve opened the code that we want to debug, the next thing to do is to create a debug configuration.
On the left side pane, click the “debug” icon
Then click the small “configure” icon

As a result, a launch.json file is generated and opened to define the launch configuration for the current project

The important setting to configure here is the “program”:
Which program should be launched by the debugger?
Answer: the FaaS runtime
More precisely: the cli.js of the FaaS SDK
How to find out the path?
The previous blog told us to install the FaaS SDK globally (with -g option)
As such, it is located in our home dir.
Where?
That can be figured out with the following command
npm list -g
This command lists all globally installed packages and it prints the location
The result might take a bit long, but it would look like this:

Based on this output, in my example (Windows 10) the path to the cli.js looks as follows:

C:\Users\myUser\AppData\Roaming\npm\node_modules\@sap\faas\lib\cli.js

However, we cannot just copy&paste this path, because the launch configuration is a json.file which treats the backslashes as escape character
As such, we have to adapt the path and enter as value for the “program” property in the launch.json file

 "program": "C:\\Users\\myUser\\AppData\\Roaming\\npm\\node_modules\\@sap\\faas\\lib\\cli.js", 
 

Note:
See appendix for alternative

What else?
Next we need the “args” property. It contains information about command line arguments.
Remember how we learned in previous blog to use the command faas-cli run?
It is the same:
The faas-sdk command to run a function is : run
As such, we enter an array with one entry for the “args”-property:

"args": [
   "run"
]

Anything else?
Last setting (optional ): enter a name of your choice for the launch configuration,
e.g.:

"name": "FaaS local debug",

Finally, the whole launch.json file looks as follows on my machine:

{
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "FaaS local debug",
            "program": "C:\\Users\\myUser\\AppData\\Roaming\\npm\\node_modules\\@sap\\faas\\lib\\cli.js", 
            "args": [ 
                "run" 
            ]
        }
    ]
}

Note:
Make sure to replace the myUser with your user.
Ah – and BTW, make sure to adapt the whole path to the FaaS installation according to your system, if required

Note:
After creating and configuring the launch configuration json file, make sure to save it
This file has now been saved in your project root folder as .\vscode\launch.json

Now the name of our launch configuration has been entered in the drop down field:

Start debugging

To start debugging, a launch configuration has to be selected.
Then the green arrow has to be pressed
The debugger runs the configured program
The Functions runtime is started.
We can see the log output (which we already know) to the debug console

Now that the FaaS runtime is up and running and listening, we can invoke the function.
The URL is given in the debug console, in my example:
http://localhost:8080/myfun

STOP
Before we invoke the function, it makes sense to add a breakpoint
Otherwise this whole blog doesn’t make any sense
To add a breakpoint, open the function file and do single click in the breakpoint area:

Now call the URL in a browser
Then change back to the IDE
You can see that the execution has stopped as desired and you can proceed with debugging

End debugging

That’s already all about debugging
Almost
I hope you’re not disappointed due to the fact that “debugging” means “debugging local” only.
If yes:
Please switch on your internet and like this blog – to encourage the development team to develop more functionality in functions
If no:
Please like this blog just, to motivate the team

Quick Giude

  • Debug local FaaS execution
  • Debug configuration contains:
    “program”: “….\\node_modules\\@sap\\faas\\lib\\cli.js”,
    “args”: [ “run” ]
  • Launch program e.g. via F5
  • Invoke function in local browser

Appendix: Alternative Configuration

In above chapter I described the – what I thought – easier way to create a debug configuration:
Using the globally installed FaaS SDK as program to be launched

The better way:
Have the FaaS SDK in local project folder and point to it via variable

1) Add the FaaS SDK as “devDependency” to your package.json

{
  "devDependencies": {
    "@sap/faas": "latest"
  }
}

2)Install FaaS SDK:

Run npm install in the project root folder
As a consequence, a node_modules folder has been generated in the project root folder and it contains the FaaS SDK

3) Create Debug Config

Now the debug configuration can point to the FaaS SDK in the project.
The path to the program is now relative to the project
So we can use a variable provided by the IDE.
In my example, the whole debug configuration looks like this:

{
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "FaaS local debug",             
            "program": "${workspaceFolder}\\node_modules\\@sap\\faas\\lib\\cli.js", 
            "args": [ 
                "run" 
            ]
        }
    ]
}

One big advantage:
This config doesn’t contain hardcoded information.
As such, you can reuse it for all projects, upload in GIT, etc

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.