Run tensorflow in Jupyter notebook on AWS
It makes a lot sense to develop a web app on your own laptop, rather than on AWS, and later on deploy it to an AWS server. However, a deep learning project often requires tuning a lot of hyper parameters and even the model architecture, which is quite difficult without training the model on a GPU and get some quick feedback. If you are going to use AWS, such as a p2.xlarge
instance, for your deep learning project, you probably need an easy and reliable way to access to python and tensorflow/keras/theano on the instance.
There are a bunch of methods to run python/tensorflow on AWS. Just to list a few here:
- Jupyter notebook through ssh local forwarding
- Jupyter notebook through http protocol
- PyCharm can also be configured to use remote interpreter, https://minhoryang.github.io/en/posts/connect-aws-ec2-instance-with-pycharm-professional/
Method 2 is my preferred way, and I will show both 1 & 2 here. Either way, you will have to install Jupyter on your AWS server:
conda create -n py2
source activate py2
conda install jupyter
Method 1:
Jupyter notebook is based on server-client structure. It uses two-process kernel architecture based on ZeroMQ and Tornado, which compose a light weighted web server. So it can run like a web app:
jupyter notebook --no-browser —-port 8888
This will actually generate a token in the terminal for remote ssh local forwarding to log in.
Then on your laptop, run:
ssh -i your_aws_key.pem -NL 8888:localhost:8888 ubuntu@your-server-ip
Open browser on your laptop and type in url: localhost:8888. You will need the token (mentioned above) to connect to your Jupyter nbserver.
Method 2:
To access Jupyter notebook server through http protocol, you will first need to generate a configuration file by running:
jupyter notebook --generate-config
This will create a file at ~/.jupyter/jupyter_notebook_config.py
, with lots of specifications, most of which are commented out. You only have to uncomment a few of them and configure them to the desired values, including certificate, SSL key, interface IP for the public server, password, default not to open browser, and service port. It should eventually look like something as the following:
# Set options for certfile, ip, password, and toggle off
# browser auto-opening
c.NotebookApp.certfile = u’/absolute/path/to/your/certificate/mycert.pem’
c.NotebookApp.keyfile = u’/absolute/path/to/your/certificate/mykey.key’
# Set ip to ‘*’ to bind on all interfaces (ips) for the public server
c.NotebookApp.ip = ‘*’
c.NotebookApp.password = u’sha1:<your hashed password>’
c.NotebookApp.open_browser = False
# It is a good idea to set a known, fixed port for server access
c.NotebookApp.port = 8888
To create a password for the notebook server run jupyter notebook password
in terminal and enter the password twice. The hashed password will be automatically saved to ~/.jupyter/jupyter_notebook_config.py
:
c.NotebookApp.password = u’sha1:<your hashed password>'
Then configure the Jupyter nbserver to use SSL with a web certificate, so that the hashed password is always encrypted when sending from the browser. Generate a pair of self-signed certificate and key using openssl
:
openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mykey.key -out mycert.pem
This will prompt to asking you some general questions, which are to be incorporated into the certificate request (Distinguished Name or a DN) and eventually create two files mycert.pem
and mykey.key
.
Now start the Jupyter notebook server:
jupyter notebook
Access to the Jupyter nbserver from web browser on your laptop: `http://your-server-ip:8888`. You will be asked to enter your password.
On your AWS server, you also have to update the inbound rules to allow traffic from ports 80 and 8888.