Debugging serverless with typescript

If you are a developer and you are reading this blog, I bet you are already frustrated with massive code and want to learn how to debug when something goes wrong…

Hence without wasting any time, let’s get started!

Create a serverless project

Since this article is about debugging, I am not going to create the serverless project from scratch, instead, I will be using aws-nodejs-typescript template.

serverless create –template aws-nodejs-typescript –path my-service

Also, as we are going to debug serverless on our local machine, we need to install serverless-offline plugin to run serverless locally as well as an IDE with debugger support.

In this article, I will be using Visual Studio Code hence to follow along, please make sure you also install the same.

Configure Visual Studio Code to enable debugging

Configuring VS code for serverless debugging is a two-step process.

Step-1: configure “package.json” file.

Include the following code snippet in the package.json file

“scripts”: {

  “start”: “./node_modules/.bin/serverless offline -s dev”,

  “debug”: “export SLS_DEBUG=* && node –inspect ./node_modules/.bin/serverless offline -s dev”

}

Now check out the debug section and understand what is it trying to do

export SLS_DEBUG=*

This tells us that the serverless offline plugin will respond to the overall framework settings and output additional information to the console in debug mode. In simple words, the logging will be verbose.

node –inspect ./node_modules/.bin/serverless offline -s dev

This tells the node to execute serverless and run the offline plugin with the stage dev in debug mode.

Step-2: Tell VS code to run this code and attach a debugger to it

Click on the debug symbol

Now click on the setting button to setup a launch configuration

Replace the default code in the launch.json with the following code snippet

{

    "version": "0.2.0",

   "configurations": [

       {

           "type": "node",

           "request": "launch",

           "name": "Debug Serverless",

           "cwd": "${workspaceFolder}",

           "runtimeExecutable": "npm",

           "runtimeArgs": [

               "run-script",

               "debug"

           ],

           "outFiles": [

               "${workspaceFolder}/.webpack/service/handler.js"

           ],

           "port": 9229,

           "sourceMaps": true

       }

   ]

}

To learn more about above code snippet, check this link

That’s it. Now you are all setup to start debugging

Select the configuration named “Debug Serverless”  and hit the play button or press F5, This will run the serverless locally with debugger attached.

Open postman and send a request to the “hello” lambda. Now you should see something like this

Cheers 🙂

Feel free to post your query in the comment section below.

About CauseCode: We are a technology company specializing in Healthtech related Web and Mobile application development. We collaborate with passionate companies looking to change health and wellness tech for good. If you are a startup, enterprise or generally interested in digital health, we would love to hear from you! Let's connect at bootstrap@causecode.com
Have you subscribed to our blogs and newsletter? If not, what are you waiting for?  Click Here

Leave a Reply

Your email address will not be published. Required fields are marked *

SUBSCRIBE!

Do you want to get articles like these in your inbox?

Email *

Interested groups *
Healthtech
Business
Technical articles

Archives