AWS EC2 instance files or code into Visual Studio Code

We can connect/open AWS EC2 instance files / code using Visual Studio.

Here are the steps needed for this.



Get your private key (.pem file) for your EC2 instance

Make sure you set read-only permission to your private key

chmod 400 my-key-pair.pem

Copy file into ssh path

cp my-key-pair.pem ~/.ssh/my-key-pair.pem

Add following into ~/.ssh/config file

Host #NameOfYourServer_Here
    HostName #IPAddreess_Here
    User ubuntu
    IdentityFile ~/.ssh/my-key-pair.pem
Example
Host RameshEC2Machine
    HostName 40.76.58.212
    User sana
    IdentityFile ~/.ssh/my-key-pair.pem

Install the extensions required in VS Code

Extension name is Remote Development and can be installed with this link Remote Development

Once above extension installed, you will see below green icon in your vs code bottom left side

Click on the icon and it will open a dialog box and select Connect to Host option

This will display you all the hosts configured in your ~/.ssh/config file

Select the name of your sever

Now you will have access to your entire EC2 instance code / files in VS code for editing.

Once you're connected to your SSH host, you can interact with files and open folders on the remote machine. If you open the integrated terminal (⌃`), you'll see you're working inside a bash shell while you're on Windows.

You can use the bash shell to browse the file system on the VM. You can also browse and open folders on the remote home directory with File > Open Folder.

Play with your code! Enjoy.

Thanks!