
Introduction
Visual Studio Code is without a doubt my favorite IDE. It’s simple, intuitive user interface makes it my go-to editor for data science projects.
It’s so good in fact, that I refuse to part ways with it even when I am working on remote servers.
Is this a problem? No, not at all!
After all, Visual Studio Code can be configured to allow users to work on remote servers. Best of all, this takes no more than 10 minutes!
This is useful for projects that require compute or storage that isn’t provided by your local machine. I’ve come to rely on this feature when I need to work with large datasets.
If you’re in a similar position and wish to process big data or train deep learning models while still enjoying the comforts of Visual Studio Code, this tutorial will show you how to use this IDE to connect to your AWS EC2.
Case Study
As part of the tutorial, we will be moving a folder containing two files to an EC2 instance: a Python file named random_number.py
and a requirements.txt
file that contains all of the dependencies for the Python script.
After the files are moved to the remote server, the random_number.py
script will be executed.
The random_number.py
executes a simple task: print a random number.
Step 1 – Launch the Instance
First, let’s launch the EC2 instance that we will be connecting to later on.
Feel free to choose the instance type to whatever suits your needs, but show some consideration when you choose the following:
1. AMI: You can use any AMI that you want, but keep it in mind, as it will affect the values you enter for the configuration in Visual Studio Code later on.
The EC2 instance for this demo will be launched with an Ubuntu AMI.

2. Key pair: A key pair will be needed to connect to the instance later on. So, keep it secured in a secure location.
The key pair for this instance is named "demo_kp.pem" and will be stored in the downloads folder.

Step 2 – Configure Visual Studio Code
We will be accessing the EC2 instance using secure shell (SSH), a network protocol that enables two computers to securely communicate and share information with one another.
To enable Visual Studio Code to use SSH, we will need to first install the required extension. Go to the marketplace by clicking on the extensions button on the left or by pressing Ctrl+Shift+X. Type in "Remote – SSH" and select the first entry.

Install the extension if it hasn’t already been installed.

Next, open the remote window by clicking the green icon on the bottom left of the window:

Select "Connect to Host".

Then, select "Configure SSH Hosts" (not the row highlighted in blue) :

Select the config file for this computer, which is the first one in this case.

After opening the config file, enter the details for the following keywords: Host, HostName, User, and IdentityFile.
For the case study, the config file will look like the following.
Host demo-host
HostName ec2-18-218-169-69.us-east-2.compute.amazonaws.com
User ubuntu
IdentityFile C:/Users/aashi/Downloads/demo_kp.pem
Here’s a quick breakdown of the keywords and what you should write next to them.
1. Host: This is a name given by the user. It can be anything you want.
The host for the case study will be called "demo-host".
2. HostName: The HostName should be the public DNS of the instance. To find the DNS of your instance, first, select the instance in the EC2 console.

Then, click on "Connect" and switch to the "SSH clien"t tab. The public DNS will be provided in the instructions.

3. User: This is the username of the instance. The username depends on the AMI selected for the EC2 instance. If you’re unsure of what the user should be, please visit the AWS documentation.
In my case, the AMI used to launch the instance is Ubuntu, so the user will be "ubuntu".
4. IdentityFile: This is the file path of the private key that is assigned to the EC2 instance. For this case study, the value assigned to this keyword is the path to the "demo_kp.pem" file.
After putting all of the pieces together, the config file should look like the following:

Save the config file after making all changes. You are now ready to connect to this EC2 instance.
Note: The public DNS of the instance will change every time you start the instance. So, whenever you get our instance running, you will need to update your config file by changing the HostName value to the new public DNS.
Step 3 – Connect to the Instance
Once again, select the green button on the bottom left and select "Connect to Host".

This time, you should be able to see your created host.

After you select your created host, a new window should pop up, which should give you direct access to your remote server!

If you look at the bottom left of the window, you should be able to see the name of your created host. Furthermore, you can confirm the connection to the instance by using the terminal.

Now that you have access to the remote server, you can write code and navigate directories in the server with ease using Visual Studio Code!
Step 4 – Set Up the Environment in the Instance
So, we’ve successfully connected to the EC2 instance using Visual Studio Code.
However, the job isn’t done yet. After all, we need to set up the environment in order to use the random_number.py
and requirements.txt
files in the server.
For the case study, we will make the following installations in the terminal of the remote server:
sudo apt-get update
sudo apt-get install python3-pip
sudo pip3 install virtualenv
With these installments, we can create a virtual environment in which we can operate.
Next, we will get the files we need to run the random_number.py
file. There are a few ways to achieve this.
First, since we are using Visual Studio Code, we can simply copy and paste the scripts from our local machine to the scripts in the remote server. Remember that we are not bound by the limitations of the command line interface.
That being said, manually copying text from one file to another can be cumbersome. Instead, it would be preferrable to copy files to the remote server using secure copy protocol (SCP).
Using SCP, we can copy an entire directory to the remote server with just one command!
The syntax for copying an entire folder is as follows:
scp -i <path/to/key_pair.pem> -r <path/to/folder_to_copy> <user>@<public DNS>:<path/to/destination>

Alternatively, if you’re just looking to copy a single file, you can use an SCP command with the following syntax:
scp -i </path/key-pair-name.pem> </path/file_to_copy> <username>@<instance_public_dns>:<destination_path>
Similar to configuring Visual Studio Code, using SCP requires the users to know the path to their key file, their user, and their public DNS. Use the same information that was entered in the config file in Visual Studio Code.
Now, our remote server should have all the files we need!

Let’s create a virtual environment named test_venv
and enter it.
python3 -m venv test_venv
source test_venv/bin/activate

Inside this virtual environment, install the packages in the requirements.txt file.
pip3 install -r requirements.txt

Finally, we can run our random_number.py
file!
python3 random_number.py

Final Remarks

If you originally believed that working on remote servers meant using boring editors like vi, I hope this article has changed your mind.
With this new skill, fans of Visual Studio Code will be able to take on more computationally intensive tasks like deep learning projects without having to adopt a less preferred IDE.
Finally, since you are now working with EC2 instances, remember that your instance’s public DNS changes every time you start it, so you will have to update the config file in Visual Studio Code (and any commands that use the public DNS) every time you spin up the instance.
Thank you for reading!