Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Connect EC2 Instance with VSCode Directy 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": "/"
}
like image 343
Anandan K Avatar asked Jan 28 '19 12:01

Anandan K


People also ask

How use PEM file AWS?

To connect to your instance using SSH In a terminal window, use the ssh command to connect to the instance. You specify the path and file name of the private key ( . pem ), the user name for your instance, and the public DNS name or IPv6 address for your instance.

How to connect to EC2 using SFTP?

Go the folder containing the .pem file of your EC2 instance. Select the .pem file. You can see that the key file is added successfully for your SFTP connection. Finally, Press Ok. In the next step, you’ll connect to EC2 file directory using Filezilla and SFTP.

How to SSH into VSCode from an EC2 instance?

2) A running SSH server preferrable a linux server running on an EC2 instance. 3) An SSH Client on your local system. a) Install the Remote extension pack on your vscode. b) In VSCode, press F1 to open the command palette, and type connect.

How to import PEM files from EC2 to FileZilla?

First, Open your Filezilla Client installed in the previous step. Next, Press Edit in the Menu. Press Settings in the sub menu. Next, select SFTP under Connection from the Select Page in the left tab. Next, Press the Add key file button. Go the folder containing the .pem file of your EC2 instance.

How to edit files on EC2 instance in VS Code?

You can click “open folder” in the file window and start working like you normally would. And that’s that, you’re ready to edit files on your EC2 instance, in VS Code.


4 Answers

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.

like image 145
Firsh - justifiedgrid.com Avatar answered Oct 22 '22 04:10

Firsh - justifiedgrid.com


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": ""
    }
}
like image 34
Niroshan Ranapathi Avatar answered Oct 22 '22 05:10

Niroshan Ranapathi


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
}
like image 2
Riswan Avatar answered Oct 22 '22 06:10

Riswan


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": "./"
    }
}
like image 2
fireDevelop.com Avatar answered Oct 22 '22 05:10

fireDevelop.com