'How to Connect EC2 Instance with VSCode Directly using pem file in SFTP

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": "/"
}


Solution 1:[1]

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": ""
    }
}

Solution 2:[2]

If you want to connect your VS Code to AWS EC2, use the Remote Development extension pack and use it for SSH. The .pem file and the standard SSH config file will reside at:

C:\Users\YourName\.ssh\

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

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

Solution 3:[3]

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
}

Solution 4:[4]

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": "./"
    }
}

Solution 5:[5]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Niroshan Ranapathi
Solution 2
Solution 3 Riswan
Solution 4 fireDevelop.com
Solution 5 begood