How to connect with Jupyter Server running on AWS EC2
Published: 2022-06-20 08:52:32

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/twlrgk5olufcwl710ovn.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lmgjntcg2i0z6jwuh5f6.png)
 
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eemdg2gx37k1uw38vzo9.jpg)
 
- Select AMI (Amazon Maintained Image), I am using Ubuntu.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xzi2kxfl1odm5cu5js3g.png)
 

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b8cgsyehhhejymv0gcr6.png)
 
- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dypfavv65viks0so3ptz.png) 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ofu6fnemxupfkc7w7zcx.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m98hg9twdy8rr7neqdtc.png) 

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q9rhaxpib5ela1i8ek79.png) 

## 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/
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gtpz99gdrjnaikkd0fim.png)
 

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

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
```

- Run the installer
```
$ sh ./Miniconda3-latest-Linux-x86_64.sh
```

- Follow the installation process & complete it.

- Run conda init configuration to ensure you have added conda to your path
```
$ conda init bash
```

- 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
```

- Start Jupyterlab server
```
$ jupyter lab
```

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/voqe4wer0d1tsi0trpvc.png) 

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
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gxbmk31wo3brd5a3jnt3.png)
 

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i7xzvdrh0jvpnz7n4iqc.png)
 
Congrats, now you have gained access to your jupyter server running on the EC2 instance.

Hope you find this useful. Happy coding !!!

<a href='https://ko-fi.com/I3I3ZKLOC' target='_blank'><img height='36' style='border:0px;height:36px;' src='https://storage.ko-fi.com/cdn/kofi2.png?v=3' border='0' alt='Buy Me a Coffee at ko-fi.com' /></a>