Run Jupyter Notebook on AWS EC2 Instance

By | February 5, 2018

Run Jupyter Notebook on AWS EC2 Instance

Data scientists largely prefer working on Jupyter Notebook for number of reasons. It makes Python code more visible and easy to interpret. Simultaneously with programming, we can visualise our data using different Python libraries. I personally enjoy working in this environment. It let’s me visualise each step of my program. For example, you can see how an image array looks like. You can also visualise images, sounds in your notebook. Moreover, it allows you to use hypertext inline. I find this very useful as I can write explanation to each line of code, what exactly it does and how I achieved it. All these features together allows you to keep your code in the form a journal. A journal that has program, description, images and results.

In this blog, I describe how to set up Amazon Web Service Instance. In simple terms, it is like a virtual computer that is running on Amazon’s server and we, instead of running our Jupyter Notebook on our local computer, run it on that server. Amazon calls it EC2 instance. To set it up, please follow the steps described below:

Step 1: Create AWS account

The first step is to create an AWS account. You can click this link and sign up there.

Step 2: Get your AWS instance running

On AWS console, click on EC2 as shown in the image.

AWS instance EC2

AWS instance EC2

Now, to create an instance, click on Launch Instance button.

Launch Amazon Instance

Launch Amazon Instance

This will redirect you to the following page. Select Amazon Linux AMI.

Amazon Linux AMI

Amazon Linux AMI

Now, we have to select type of instance we want to use. Instance named ‘t2.micro’ is eligible for free tier. So, we can select that one. If you want to perform training on deep learning models then you can select others as per your requirements. Make sure to check the per hour charges in that case. Mostly, for performing deep learning p2.xlarge is good enough. Once you are done selecting, click ‘Configure Instance Details’ button on right bottom corner.

Amazon Instance Selection

Amazon Instance Selection

Keep hitting the ‘Next’ button until you reach step 6 ‘Configure Security Group’. Make sure each rule matches with the details below. Use ‘Add Rule’ button to add new rules.

Configure Security Group AWS

Configure Security Group AWS

Click ‘Review and Launch’ and then ‘Launch’ button. If you are doing this for the first time, you will need to create a new key-pair and download it on your computer. If you have an existing one then select it and hit ‘Launch’ button.

Key-Pair

Key-Pair

Next click ‘View Instance’ button in bottom right corner.

AWS Instance running

AWS Instance running

Bingo! You have got it running now.

Step 3: Connect to instance via Terminal

As our instance is running on server, now we need to connect to the instance via our computer. Open Mac Terminal and type the following command:

AWS connect

AWS connect

I have moved my key-pair .pem file to root directory on Mac. I also would suggest you to do the same in order to run the command like above.

Note: You will need to add public DNS of your Amazon instance. This you can find at the bottom right corner of your instance running screen next to ‘Public DNS(IPv4)’. Copy it from there and paste it after ec2-user@ ‘public DNS of your Amazon instance’.

AWS connect

AWS connect

AWS connected

AWS connected

Hurray! now you are connected to Amazon instance running on server via your terminal. Isn’t it cool?

Step 4: Running Jupyter Notebook

Let’s download Anaconda. Paste the following command in your terminal window and hit enter.

Once it is done, paste following command and hit enter. It will ask you to agree license terms. Follow instructions on terminal screen.

Now, configure environment.

Create Jupyter Notebook password:

Jupyter Notebook password

Jupyter Notebook password

Enter any password you want and it will generate a password like above. You need to save it somewhere for further use.

Create config file and certificates. You need to perform it for https. Use the following block of commands.

Now, edit config file using following commands:

Once file is opened in the editor, press ‘i’ to insert following lines of code:

Press esc, then shift+z and shift+z again.

We are about to finish. Now, make directory for notebooks.

Now, type down command to start Jupyter Notebook and hit enter:

Jupyter Notebook

Jupyter Notebook

You can launch Jupyter Notebook on your web browser by accessing the link as https:// + public DNS of your Amazon instance:8888

AWS Jupyter Notebook

AWS Jupyter Notebook

Jupyter Notebook running

Jupyter Notebook running

You will be asked to enter password. Here, you are supposed to enter the password that you typed in. That means, the non-encrypted one.

Whoa! It’s all done. Now you can run your deep learning, data science projects on Amazon server using Jupyter Notebook.