1

I created a simple test case test.py with:

print('Hello)

Python 3.7.9

I have an AWS Security Group that whitelists my IP address on port 22. I connect with:

ssh -v -2 -L 5678:localhost:5678 -i "~/.ssh/myssh.pem" ec2-user@<external ec2 IP address>

Once logged in I launch the test.py as follows:

$ python3 -m debugpy --listen <ec2 internal IP here>:5678 --wait-for-client -m test  

When I try to connect from VS Code (using F5) I get:

debug1: Connection to port 5678 forwarding to localhost port 5678 requested.
debug1: channel 3: new [direct-tcpip]
channel 3: open failed: connect failed: Connection refused
debug1: channel 3: free: direct-tcpip: listening port 5678 for localhost port 5678, connect from 127.0.0.1 port 35636 to 127.0.0.1 port 5678, nchannels 4

I also tried using this method in a more complex test case, with a print statement prior to this code, it does print to console so I know the application is launching correctly.

# Allow other computers to attach to debugpy at this IP address and port.
debugpy.listen(('<my internal ec2 IP here>', 5678))

# Pause the program until a remote debugger is attached
debugpy.wait_for_client()

In VS Code I have this launch.json

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
    {
        "name": "Python: Remote Attach",
        "type": "python",
        "request": "attach",
        "connect": {
            "host": "localhost",
            "port": 5678
        },
        "pathMappings": [
            {
                "localRoot": "${workspaceFolder}",
                "remoteRoot": "/home/ec2-user/webapp"
            }
        ]
    }
]
}

Ideas on why I get Connection refused?

Olddave
  • 397
  • 1
  • 2
  • 12

1 Answers1

1

The issue here is the fact that the EC2 console defines the internal IP address for the EC2 instance and that is what I specified. But what is actually required is the localhost IP, 127.0.0.1, as the ssh tunnel 'emerges' at that address.

Olddave
  • 397
  • 1
  • 2
  • 12
  • I can't thank you enough for this. So it looks like if your remote server is running on AWS instance then the server needs to listen on 127.0.0.1 instead of private EC2 instance address as mentioned in https://code.visualstudio.com/docs/python/debugging#_remote-script-debugging-with-ssh guide – dsingh Apr 07 '22 at 14:47