Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug Serverless Offline in Visual Studio Code using another port?

I have two Serverless Offline "servers" which I need to run locally at same time.

So I need to change the port of one of the servers.

I run a server using Visual Studio Code debugger. The configs of the servers are in launch.json files.

How can I change the port of a Serverless Offline application so that I can run it in parallel with another Serverless Offline application using VS Code debugger?

like image 519
Natanael Avatar asked Jun 25 '18 23:06

Natanael


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 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.

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.


2 Answers

If you are using windows, update the vscode launch.json and package.json as below :

// launch.json
{

    "version": "0.2.0",

   "configurations": [

       {

           "type": "node",

           "request": "launch",

           "name": "Debug Serverless",

           "cwd": "${workspaceFolder}",

           "runtimeExecutable": "npm",

           "runtimeArgs": [

               "run",

               "debug"

           ],

           "outFiles": [

               "${workspaceFolder}/handler.js"

           ],

           "port": 9229,

           "sourceMaps": true

       }

   ]

}

// package.json
....
"scripts": {
    "debug": "SET SLS_DEBUG=* && node --inspect %USERPROFILE%\\AppData\\Roaming\\npm\\node_modules\\serverless\\bin\\serverless offline -s dev"
  }

If on linux your debug script will be:

// package.json
....
"scripts": {
    "debug": "export SLS_DEBUG=* && node --inspect /usr/local/bin/serverless offline -s dev"
  }
like image 188
bhavesh vyas Avatar answered Oct 03 '22 07:10

bhavesh vyas


Solved by adding the following lines to the serverless.yml file:

custom:
    serverless-offline:   ## add this two lines
        port: 4000        ## bellow "custom:" line
like image 44
Natanael Avatar answered Oct 03 '22 07:10

Natanael