26

I'm trying to connect EC2 Instance directly from VS Code using SFTP. I can able to connect other ftp service using [password] type, but for EC2 instance i'm having only .pem file. Expecting something like this but using public key .pem file

{
"protocol": "sftp",
"host": "localhost",
"port": 22,
"username": "username",
"remotePath": "/"
}
Anandan K
  • 1,380
  • 2
  • 17
  • 22

5 Answers5

52

If you want to connect your VS Code to AWS EC2, use the Remote Development extension pack (if it's not installed already). The .pem private key file and the standard SSH config (extensionless text file) will reside at:

C:\Users\YourName\.ssh\aws-example-user.pem

C:\Users\YourName\.ssh\config

Make the latter have contents like this:

Host aws-ec2
     HostName example.dev
     User example
     IdentityFile "C:\Users\YourName\.ssh\aws-example-user.pem"

You can specify nonstandard port by Port 2222 and HostName can be an IP address (on AWS fixed IPs are called elastic). The key file (sometimes extensionless) has the words PRIVATE KEY in it. New: use the ed25519 type of key if it fails to connect. This is not just for AWS EC2, but any similar system such as Ubuntu in a VM or even a Raspberry Pi.

PS: I wrote an article detailing the whole VS Code to AWS EC2 setup.

Side note: VS Code server needs about 2GB of RAM to run smoothly, if you have less, try zram.

16

Try this config.json on VScode

{
    "remotePath": "/",
    "host": "<IP-OR_EC2-INSTANCE-HOST-NAME>", 
    "username": "USERNAME",
    "password": "PASSWORD",
    "port": 22,
    "secure": true,
    "protocol": "sftp",
    "uploadOnSave": true, 
    "passive": false,
    "debug": true,
    "privateKeyPath": "<PATH-TO-PEM-FILE>",
    "passphrase": null,
    "ignore": [
       ------
    ],
    "generatedFiles": {
        "uploadOnSave": false,
        "extensionsToInclude": [],
        "path": ""
    }
}
Niroshan Ranapathi
  • 3,007
  • 1
  • 25
  • 35
2

Open the sftp.json config file by pressing ctrl + Shift+ P then type SFTP:Config and edit "host", "privateKeyPath" then save the json file

{
"name": "GIVE ANY NAME",
"host": "ec2-.........compute.amazonaws.com",
"protocol": "sftp",
"port": 22,
"username": "ec2-user",
"privateKeyPath": "SPECIFY YOUR PATH/FILENAME.pem",
"remotePath": "/",
"uploadOnSave": true
}
Riswan
  • 334
  • 3
  • 5
2

In my mac OS Catalina 10.15 with Visual Studio 1.39.0 works this, usign a Key Pem:

Note: privateKeyPath, is the path of the key.pem in your computer.

{
    "name": "id0000533-test",
    "protocol": "sftp",
    "host": "ec2-12-123-123-123.eu-west-1.compute.amazonaws.com",
    "remotePath": "/var/www/laravel",
    "privateKeyPath": "../keypem/id0000533-test.pem",
    "username": "admin",
    "port": 22,
    "secure": true,
    "uploadOnSave": true,
    "passive": false,
    "debug": true,
    "ignore": [
        "\\.vscode",
        "\\.git",
        "\\.DS_Store"
    ],
    "generatedFiles": {
        "uploadOnSave": true,
        "extensionsToInclude": [],
        "path": "./"
    }
}
0

New VSCode Remote-SSH gives errors, a combination of 2 things worked for me:

  1. Revert to Remote-SSH 0.49 or before

  2. Change the permissions on the pem file to list user as the only owner and remove inheritance for other users.

After tons of trial and error Remote-SSH worked again, this time to Amazon EC2 running Ubuntu.

begood
  • 19
  • 3