https://serverless-stack.com/ logo
#help
Title
# help
j

Jason S

05/23/2022, 3:41 AM
Hey @Jay, @Frank, I've successfully followed the Guide and build the Tasks app. I'm using Python for my Lambda functions. How do I create a config to use the VSCode debugger for my Python Lambdas?
k

Klaus

05/23/2022, 7:27 AM
@Jason S I had exactly the same problem and got it working by checking out the VSCode debugging help
Here are the steps: 1) install
debugpy
with pip 2) Add a launch configuration for Python in
.vscode/launch.json
Copy code
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Debug SST Start",
      "type": "node",
      "request": "launch",
      "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/sst",
      "runtimeArgs": [
        "start",
        "--increase-timeout"
      ],
      "console": "integratedTerminal",
      "skipFiles": [
        "<node_internals>/**"
      ],
      "env": {}
    },
    {
      "name": "Python: Attach",
      "type": "python",
      "localRoot": "${workspaceFolder}/backend/functions",
      "request": "attach",
      "host": "localhost",
      "port": 5678
    }
  ]
}
3) modify the python startup related code in node_modules/@serverless-stack/core/dist/runtime/handler/python.js and replace
"-u"
(line 76 for me) by
"-m", "debugpy", "--listen", "0.0.0.0:5678" , "--wait-for-client",
4) Make sure you increase the timeout for sst in package.json
Copy code
...
  "scripts": {
    "start": "sst start --increase-timeout", // <--
     ...}
the
--wait-for-client
option means you now have to launch the debugger (F5 for me on Windows) for the function to run
@Jay @Frank could you perhaps later include an option to read an extra parameter when starting sst that could be passed to the python runtime handler and allow an easy choice for a user to wait for the debugger without my hack 🙂?
j

Jason S

05/23/2022, 7:49 AM
Klause thanks mate for this. I would have never sorted it out.
When I run as described,
debugpy
returns the following:
RuntimeError: Can't listen for client connections: [Errno 48] Address already in use
I changed the port thinking I had something running on
5678
from previous attempts with no success
Or are you pointing to a specific function in your
launch.json
?
k

Klaus

05/23/2022, 7:54 AM
1. make sure that you are not debugging SST already 2. for linux environments, there is an easy way to kill app listening on a port - https://stackoverflow.com/questions/9346211/how-to-kill-a-process-on-a-port-on-ubuntu 3. for windows there is something similar https://stackoverflow.com/questions/39632667/how-do-i-kill-the-process-currently-using-a-port-on-localhost-in-windows
but in worst case, you can also specify a different port (it needs to be the same in the command line parameter and your debug configuration)
The root in launch.json also has to match the folder setup you have (I just used the one from the python starter example)
j

Jason S

05/23/2022, 7:59 AM
Hm nothing is running at that port. I tried switching ports to something else - no luck.
I assume I need to re-run
npx sst start
after making the changes to
python.js
, right?
k

Klaus

05/23/2022, 8:10 AM
I think this won't work as it's creates a new repo and reinstalls node modules
after the change to
node_modules/@serverless-stack/core/dist/runtime/handler/python.js
you just run
npm run start
(whihc use the modified file in the current project
the parameters for running the function are only generated when sst starts, so a new run is needed
I also updated the instructions above with 4) (just in case you didn't increase the timeout for SST in package.json yet)
j

Jason S

05/23/2022, 8:31 AM
Are you putting any
debugpy
imports or method calls within your lambda function?
k

Klaus

05/23/2022, 9:35 AM
No - that's not needed with the modified
node_modules/@serverless-stack/core/dist/runtime/handler/python.js
file
An alternative would be to not modify the file + run debugpy directly in the handler itself, but perhaps it's less elegant this way - it depends a bit if you need to debug all functions or not
This is hos it would look inside the lambda function (but then you have to remove the python.js patch as they would conflict)
Copy code
# ...
# for debugging only
import debugpy
debugpy.listen(('0.0.0.0', 5678))
debugpy.wait_for_client()
debugpy.breakpoint()
@Frank I also updated the open Github Issue, with a link here, just in case someone prefers searching there
j

Jay

05/23/2022, 5:07 PM
This is great @Klaus! Thanks for this. We’ll look into it.
j

Jason S

05/25/2022, 1:38 AM
I was able to get this working by adding the import statements into the lambda function handler directly.
I then have to trigger the lambda and run the VSCode debugger at the same time. I presume this is because the
debugpy
listener is only listening during the function run.
I ran the debugger on the JS code on the python.js hander line by line. It seems like on my environment the
debugpy
listening server is not being run during the SST start procedure.
After starting SST, I run
ps aux | grep debugpy
and there is no service running.
f

Florian Fuß

06/17/2022, 6:41 PM
@Klaus I like your proposal - would be good to have debugpy included into node_modules/@serverless-stack/core/dist/runtime/handler/python.js and using a flag to switch between debug and normal mode. Can you create a PR for the change you did ?
517 Views