How to connect with Jupyter Server running on AWS EC2

mathanraj_sharma

Mathanraj-Sharma

Posted on June 20, 2022

How to connect with Jupyter Server running on AWS EC2

Nowadays Data scientists, ML Engineers, Product Managers, Customer Success Engineers, Lecturers, Students, and many other stack holders love to use jupyter notebooks for many reasons. But sometimes the task we are trying to run might be resource-consuming and we might have not enough resources on our local machine.

We can rely on AWS EC2 for such situations, we can create an EC2 instance with the preferred OS, memory & storage. Today we are going to see,

  1. How to start an AWS EC2 instance?
  2. How to connect to the EC2 instance via ssh?
  3. How to install Anaconda/Miniconda on EC2?
  4. How to connect to a jupyter server on EC2 using port forwarding?

How to start an AWS EC2 instance?

Here I am going to show you how to start an Ubuntu instance. You need to make sure you have an AWS account & few credits on it.

Step 01- Log in to the AWS console and visit the EC2 console.

Image description

Step 02- Once you are in the EC2 console click on Launch Instance

Image description

Step 03: It will take you to the page where you can configure your instance. You need to

  • Give your instance a name. I am using my-jupyterlab

Image description

  • Select AMI (Amazon Maintained Image), I am using Ubuntu.

Image description

  • Select the instance type, here I am using t2.medium for the demo. You can select an instance type with adequate memory you required

Image description

  • Create or select an existing key pair (this key will be used to shh into your EC2 instance). When you are creating a new key, use rsa encryption download the .pem file to use it with Openssh. When you click create new key pair it will automatically download the private key. Keep it safe!!!

Image description

Image description

  • Select the amount of storage you need. I am selecting 4Gb, since I am creating this only for this demo.

Image description

  • I leave the other configurations as defaults. Feel free to config them as you require.

  • Once everything is configured click Launch Instance to start your instance. On the EC2 dashboard check whether your instance status is RUNNING .

Image description

How to connect to the EC2 instance via ssh?

Hope you have launched an instance successfully. Next what you have to do is move your private key downloaded to your ssh directory. (Assume you have downloaded the key at ~/Downloads. Your key file name and extension may very. But in order to connect to your instance via Openssh, it needs to be either .cer or .pem , my key file is my-jupyterlab.cer)

$ mv ~/Downloads/my-jupyterlab.cer ~/.ssh/
Enter fullscreen mode Exit fullscreen mode

Open a terminal and run the below command to connect to your instance via ssh. You can find the public IP address of your instance from the instance dashboard. Most of the time instance name for ubuntu EC2 instances is ubuntu , you can change it as you need. You can find how to do it by playing with the instance dashboard. Let me leave you to find it with that hint.

Image description

##  ssh -i [path-to-private-key] [username@public-IP]
$ ssh -i ~/.ssh/my-jupyterlab.cer ubuntu@3.80.109.97
Enter fullscreen mode Exit fullscreen mode

It will prompt whether to add the fingerprint to your local machine. Type yes and press enter to continue.

How to install Anaconda/Miniconda on EC2?

You can install Jupyterlab on your EC2 instance in various ways. I prefer to manage my python environments using Anaconda/Miniconda. So let me show you how to install Miniconda, create a conda environment & install Jupyterlab in it (Follow below steps on your terminal which is connected to your EC2 instance)

  • Download Miniconda installer
$ wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
Enter fullscreen mode Exit fullscreen mode
  • Run the installer
$ sh ./Miniconda3-latest-Linux-x86_64.sh
Enter fullscreen mode Exit fullscreen mode
  • Follow the installation process & complete it.

  • Run conda init configuration to ensure you have added conda to your path

$ conda init bash
Enter fullscreen mode Exit fullscreen mode
  • Once it is successful exit from your ssh connection & connect to your EC2 again.

  • Create a conda environment and install Jupyterlab (run the commands one by one)

$ conda create --name jupyter python=3.8
$ conda activate jupyter
$ conda install jupyterlab
Enter fullscreen mode Exit fullscreen mode
  • Start Jupyterlab server
$ jupyter lab
Enter fullscreen mode Exit fullscreen mode

How to connect to a jupyter server on EC2 using port forwarding?

All right, now finally everything is set.

Usually, when we start a jupyter server, it will be running on http://localhost:8888/lab. But if you go to your browser and try accessing this address you will get the message Your site connot be reached . That is because in this case, the jupyter server is running on your EC2 instance’s localhost, not on your local machine.

So how can we get access to that jupyter lab via browser? Let’s understand what we have and what we don’t have

  • we have access to the EC2 instance via ssh
  • we don’t have access to the jupyter server running on our EC2
  • but our EC2 instance can access the jupyter server which is running on its localhost
  • and we have access to the localhost on our local machine

So what we can do is, we can tell our machine to listen for the traffics on our localhost:8888 and forward everything to EC2’s localhost:8888 via a secure tunnel.

This process is well known as local port forwarding .

Image description

So how do it? Ensure you have followed all the above steps and your jupyter server is up and running on EC2.

Open another terminal and run the below command

## ssh -L [PORT-on-your-machine]:[IP-jupyter-server-on-ec2]:[PORT-jupyter-server-on-ec2] [username]@[public IP of EC2] -i [path to private key]

$  ssh -L 8888:127.0.0.1:8888 ubuntu@3.80.109.97 -i ~/.ssh/mr-jupyterlab.cer
Enter fullscreen mode Exit fullscreen mode

Once you run this on your other terminal (which one you started jupyter server on EC2) you will see a message like below

Image description

Copy & paste the URL http://localhost:8888/lab?token=xxxxxxxxxxxxxxxxxxxxxx on your browser.

Image description

Congrats, now you have gained access to your jupyter server running on the EC2 instance.

Hope you find this useful. Happy coding !!!

Buy Me a Coffee at ko-fi.com

💖 💪 🙅 🚩
mathanraj_sharma
Mathanraj-Sharma

Posted on June 20, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related