How to connect Jupyter Notebooks to an AWS EC2 instance

This article describes a process to connect a Jupyter Notebook to a GPU/AWS EC2 instance. For some machine learning applications the increased speed of using a GPU may outweigh the cost. If so, connecting to an EC2 can be an easy way to run a model from a notebook file, once it has been uploaded.

In order to use AWS Compute, register and select EC2 https://aws.amazon.com/console/

Next choose an Amazon Machine Image (AMI) …these can be found by searching AWS Marketplace…. this image Deep Learning AMI (Amazon Linux) — comes with TensorFlow, PyTorch, Caffe, Caffe2, and Keras (among others) installed in Ana”Conda” environments.

Choose an instance type…some instances are recommended for use with various images. I chose p2.xlarge…(you may have to request larger instance types from Amazon)

The instance is ready to be reviewed and launched. However, in order to connect a security group, and key pair will have to be created. After clicking “Review and Launch” click edit security groups

Create a new security group, and add a rule. Most importantly change the port range to 8888

Now, name and download a key pair

“Download Key Pair”…

You should now be able to click “Launch Instance”… but first save the key pair, a .pem file to a place where you can find it

Click “View Instances” (lower right hand corner)

Once the status checks are complete the instance is running, and AWS has began charging your account!

Putty is used to connect to the instance that is now running…. it can be downloaded here: https://www.putty.org/

32 or 64bit

AWS provides a key pair as a .pem file which will now be converted into a putty file (.ptk ) with the putty generator (puttyGen)

open PuttyGen from the start menu, click “load” and navigate to the folder where the key pair file is saved. Once loaded, click on “save private key”. This creates a putty file from the .pem file that is used to connect to the instance, save this file too.

Now it is finally time to open Putty

The host name needs to be provided. Enter thehost name ….for a AWS linux image it is ec2-user@ the public DNS shown in the AWS console.

Host Name: ec2-user@ec2–34–204–107–173.compute-1.amazonaws.com

screenshot from AWS console page

Navigate to SSH → Auth (in left window of image below)

In the browse field, find the .ptk key pair file that was saved earlier.

Click yes…you are now connecting to your running GPU instance!

In the terminal run command

Then ….

Copy and paste the highlighted section, including the token into a browser

http://(ip-172-31-30-122 or 127.0.0.1) :8888/?token=c74742cdeffa38f2029175bb56eb1c1eb89b08e07f9566bc

Take the bold section and replace with the IPv4 Public IP address

Paste a link with the ipv4 address and the token into a browser http://54.157.212.253:8888/?token=c74742cdeffa38f2029175bb56eb1c1eb89b08e07f9566bc

Jupyter Notebook can be accessed!

You are not ready to upload a notebook to the GPU workspace, and run models in much less time! When you are finished make sure to terminate the instance in order to stop the charges!

Done!