Detailed Guide Connecting EC2 to VS Code — A Great Deep Learning Solution

Bobby Cheng
7 min readMay 24, 2023

--

Setting Up Amazon EC2 with VSCode

Amazon Elastic Compute Cloud (EC2) provides scalable computing capacity for developers and engineers to develop and deploy applications faster. However, it’s not solely for commercial purposes. If you are someone who is keen to run deep learning projects as a hobby, there’s a fair chance that your local machine may encounter computing troubles for compute-heavy projects. In that case, you can be a user of Amazon EC2 as well.

In this article, I’ll guide users to set up their Amazon EC2 instance and execute their Python notebook projects remotely via Visual Studio Code (VSCode).

If you are new to EC2 and VSCode, do check out the following links to learn more about them. These are my personal favourite guides that visually aid one’s understanding.

Let’s get started.

Setting up Amazon EC2

The process is easy and straightforward. Where relevant, I’ll share my learnings at each step. Here’s how to set up your Amazon EC2:

1. Log in to your AWS Console by visiting here, and clicking the top right-hand orange icon that says ‘Sign In to the Console’. You’ll be brought to a new webpage. Then, enter your AWS account email address and password. You would arrive at your Console Home.

Figure 1 — Screenshot of the AWS Console Home

2. In the search bar, type “EC2” and select the EC2 service as shown in Figure 2.

Figure 2 — Screenshot of EC2 Service search

3. Now that we are in the EC2 console, we’ll proceed to launch an instance. Look for an orange icon that spells ‘Launch instance’, and select it. A pop-up widget would appear under the orange icon once you’ve selected it. Simply select the ‘Launch instance’ option. You would be brought to the page that looks like the one in Figure 3.

Figure 3 — Screenshot of Launching Instance

4. In the following steps, the EC2 configurations I’m inputting are catered for Deep Learning projects. Personally, I wanted to run Deep Learning projects faster to minimise processing duration between each experiment and training. Hence, I am opting to use the following EC2 configurations:

A. Name and tags: Feel free to name the instance as you want it, i.e. deeplearning.

B. Application and OS Images (Amazon Machine Image): Deep Learning AMI GPU TensorFlow 2.7.3 (Ubuntu 20.04) 20220613. Simply copy and paste this AMI name into the search bar of the AMI section. You would find this AMI under ‘Community AMIs — Published by anyone’.

C. Instance Type: C5.2xlarge. I’m using this instance type for 2 reasons. The first reason is that, in my earlier trial and error stages of setting up my Ubuntu remote server with the free-tier eligible t2.micro instance type, I frequently encountered errors with the pip installation of TensorFlow. Fortunately, C5.2xlarge’s Python version came ready with TensorFlow packages. The second reason is that C5.2xlarge’s 8 vCPUs and 16GB Memory processes much faster than my local machine when it comes to my Deep Learning projects. Hence, I chose to go with C5.2xlarge. To read up and compare each EC2 instance type, I recommend visiting here.

D. Key pair (login): Here is a crucial step. If you have no existing key pair, or if you’ve lost your .pem file, you may select “Create new key pair”. A pop-up window will appear, to which you can give it a name — i.e. “practice-instance-1”. Then, set key pair type = ‘RSA’ and private key file format = ‘.pem’. After clicking ‘Create key pair’, a .pem file will be downloaded. Note — safely store your .pem file in your project folder. Its file path will come in handy later.

E. Network Settings: First, select the ‘Edit’ button on the top right hand of this section. Then, under firewall, select ‘Create security group’. Then, name and select the following settings.

  • Security group name: ‘launch-wizard-1’ (feel free to rename this)
  • Description: leave it as default.
  • Inbound security groups rules: Here, we’ll create 2 security groups. The first security group rule is based on the source type for ‘My IP’. This ensures that no one else can access your port. The second security group rule is generally for Jupyter Notebook connections.
Figure 4 — Screenshot of Inbound Security Groups Rules

F. Configure storage: Depending on your needs, you can vary the root volume. For my case, I’ve set it to ‘60GB gp3’.

Once you’ve finished the above steps, you’re ready to select the orange icon that spells ‘Launch instance’. That will bring you to a new page that shows your new instance. It’ll reflect the ‘instance state’ as either ‘pending’ or ‘started’. If it says ‘pending’, give it a moment before it reflects ‘started’. And voila, your EC2 instance is up and running; ready to be accessed remotely.

Now let’s move on to the next step of this exercise — accessing it remotely via VSCode.

Connecting VSCode to your EC2 Instance

1.In your VSCode, head to the extensions marketplace to install ‘Remote — SSH’. It lets you use any remote machine with an SSH server as your development environment; simplifying development and troubleshooting in a wide variety of situations. Once installed, a new status bar item will appear in the lower left-hand corner of your VSCode window, as circled in Figure 5.

Figure 5 — Screenshot of Remote-SSH status bar icon

2.Click on the Remote — SSH status bar icon. A pop-up window as shown in Figure 6 will appear at the top of your VSCode window. Select the option ‘Connect to Host…’

Figure 6 — Screenshot of VSCode ‘Connect to Host…’ pop-up

3.Then, select the option ‘Configure SSH Hosts…’.

Figure 7 — Screenshot of VSCode ‘Configure SSH Hosts…’ pop-up

4.Then, select the configuration file that starts with ‘/Users/..’.

Figure 8 — Screenshot of VSCode User’s SSH Config path pop-up

5.Then, you’re brought to a config.ssh file that allows you to edit information on it. It will look quite similar to the undermentioned code snippet. There are 4 fields for you to edit:

  • Host: This can be any name you wish. In my given example, I have named it ‘jupyter-deeplearning’.
  • HostName: This is the server’s host or IP. You can grab this from your EC2 instance. Ensuring that your EC2 instance is running, select it and look for the field titled ‘Public IPv4 DNS’ under its details tab. An example of it is ‘ec2–12–345–678–901.ap-southeast-1.compute.amazonaws.com’.
  • User: Because we are using a Ubuntu AMI, the username would be ‘ubuntu’.
  • IdentityFile: This will be the file path of the key pair login .pem file you downloaded earlier. It was downloaded the moment you created a new key pair login.
# Read more about SSH config files: https://linux.die.net/man/5/ssh_config
Host jupyter-deeplearning
HostName ec2–12–345–678–901.ap-southeast-1.compute.amazonaws.com
User ubuntu
IdentityFile ./key/practice-instance-1.pem

If you are unsure about the fields for User and HostName, you can find them by navigating to the “Instances” tab of the EC2 Console, right-click your running EC2 instance, and select ‘Connect’. You would find a string that looks like the undermentioned snippet. “ubuntu” will be the User and “ec2–12–345–678–901.ap-southeast-1.compute.amazonaws.com” will be the HostName.

ssh -i "practice-instance-1.pem" ubuntu@ec2-12-345-678-901.ap-southeast-1.compute.amazonaws.com

6.Save the config.ssh file. Then, click the bottom left-hand corner Remote — SSH status bar icon, click “Connect to Host…” and select the option that mirrors the Host name you’ve used in the config.ssh file. In our case, that would be ‘jupyter-deeplearning’. Upon selecting it, let VSCode load the remote server. Once you see the bottom left-hand corner Remote — SSH status bar icon reflecting like the screenshot shown in Figure 9, then you’ve connected to your EC2 Instance!

Figure 9 — Screenshot of Remote-SSH status bar icon indicating a successful connection

7.One more final step. In your VSCode, search for “Remote-SSH: Settings”. You’ll arrive at a window that looks something similar to Figure 10. Change the connect timeout value from 15 to 60. This means that VSCode will not disconnect from your EC2 Instance until 60 minutes of inactivity.

Figure 10 — Screenshot of ‘Remote-SSH: Settings’

And just like that, you are connected to your Amazon EC2 instance via Visual Studio Code!

Now what? What’s next?

Well, in my next article, I’ll cover some of the following questions users may have after connecting to EC2 via VSCode. For example,

  1. How do you copy files between your EC2 instance’s folder storage and your local machine?
  2. How do I select a Python kernel to run my ‘.ipynb’ files?

About the Author

Bobby Cheng is passionate about using data and software to create value for others. He has experience using ML (i.e. geospatial analytics, natural language processing, ensemble classifications) to research and build solutions for citizens.

In addition, as the lead data scientist for the ASEAN region of Amazon Web Services (AWS), he has experience developing software programs and engineering the new generation of ML solutions for AWS’s Commercial Sales business.

Please reach out to Bobby on LinkedIn.

--

--