Skip to content

How to set up a AWS EC2 Instance in VSCode

Posted on:January 5, 2024

I will explain how to connect to a remote OS to VSCode. The remote OS will be a ubuntu server in a AWS instance that, in my case, will be used to run lightweight ml models.

where to put the key

Table of Content

1. Create an AWS instance

First, just google AWS, create an account, and you will see the option to create an instance in the main page.

Free accounts have a free tier. Selecting everything with the “Free tier eligible” should be free, but please take care what you launch anyway and do not select a 98 Ram machine with a Tesla V100 GPU and whatnot unless you know what you doing.

Here I will set up a cheap machine (like $0.1/hour):

AWS instance summary

If the SSH key file was placed correctly, you can access the machine via ssh using the Public PIv4 address you can find in your instance summary website:

ssh -i ~/.ssh/mlops_key.pem ubuntu@{Instance public IPv4 address}

AWS connection

P.S. I did have a problem the first time I tried to connect with “permissions being too open”:

AWS instance summary

That can be solved by running chmod 600 ~/.ssh/mlops_key.pem (Source)

We would like to avoid typing the IP of the virtual OS every time we connect to the instance and simply connect typing “ssh mlops”, for example.

To create this shortcut, modify the config file under ssh or your local machine:

Host mlops
  HostName {Instance public IPv4 address}
  User ubuntu
  IdentityFile ~/.ssh/mlops_key.pem
  StrictHostKeyChecking no

E.g.:

Automatic connection

2. Install stuff in the ubuntu OS

# Update apt
sudo apt update
# Install docker and docker-compose
sudo apt install docker.io docker-compose
# To us docker without using sudo continuously
sudo usermod -aG docker $USER
# Download miniconda
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
# Run the installer
bash Miniconda3-latest-Linux-x86_64.sh -b
# Add Anaconda to the system path
cd miniconda3/bin
./conda init bash

After this, “which python” should point out to the anaconda version:

Which python

3. Configure VSCode

Open VSCode locally and install the following:

Open Command Palette(Cmd+Shift+P) and search “Connect host” and it will pop up:

Host name

It will automatically read the Host from the .ssh/config file:

Host name

And that’s it, you have VSCode connected to remote ubuntu machine.

Clicking open folder will load a tree view. From there you can easily organize files, drag&drop files from your local computer to the folders in the remote machine, or create new files. Like you would locally.

where to put the key

Please remember to stop the instance in AWS to avoid getting charged while not using it.