Hosting your own password protected Jupyter Notebook server on AWS

It’s very common to have a need to share your interactive Jupyter notebook with your colleagues. Maybe you are working on a project where you depend on interactivity to easily present information, or maybe your spooky stakeholders want to see ‘something’ on hover in your carefully crafted plots!, forcing you to avoid a github or nbviewer solution. You have an option for Google Colab notebooks, but for some reason you don’t want to use that either.

So, what to do in that case other than spinning up your own server on the cloud and open that up for your stakeholders behind a password protected firewall?

Let’s do just that. Here’s what you need to do:

  1. Spin up an AWS EC2 instance:

    I am not going to teach you how because there are so many of those just a google search away. Just go to your AWS console, create a new EC2 instance, create or edit a new security group where all incoming TCP connections are allowed to port 8888 and 443. Also make sure your outgoing connections are not interrupted.

  2. Install Anaconda.

    I had Ubuntu 18.04 with Anaconda 5.3. Go to Anaconda website, get the latest Anaconda release and execute the following code (replace with your link for latest anaonda relase .sh file).

    wget https://repo.anaconda.com/archive/Anaconda3-5.3.0-Linux-x86_64.sh
    bash Anaconda3-5.3.0-Linux-x86_64.sh

    Accepts the license agreement (don’t hurry with your ENTER button here please ;) and keep accepting the default choice until the whole installation prcess completes.

  3. Install Plotly

    Because that’s the whole point for my scenario right? bash conda install plotly

  4. Create a directory for storing notebooks

    mkdir Notebooks
  5. Create a password for your Jupyter notebook login

    ipython
    from IPython.lib import passwd
    passwd()
    Enter password: [Create password and press enter] 
    Verify password: [Press enter]
    'sha1:98ff0e580111:12798c72623a6eecd54b51c0069u0dASDAU0f0acdsdcascd'
    exit

    Copy your sha1 code and paste somewhere safe for a moment.

  6. Create a Jupyter config file.

    jupyter notebook --generate-config
  7. Open that config file.

    nano ~/.jupyter/jupyter_notebook_config.py
  8. Insert the following at the top of the file to customize your Jupyter server. Remember to replace my SHA1 hash with the one you just created.

    
    c = get_config()
    
    # kernel config
    c.IPKernelApp.pylab = ‘inline’ # for inline plotting in notebook
    
    # notebook config
    # I am not using HTTPS as it was giving me permission denied error (Err no 13)
    # on Ubuntu 18.04 and Anaconda 5.3 on an AWS EC2 instance
    
    c.NotebookApp.ip = ‘0.0.0.0’ # any IP
    c.NotebookApp.open_browser = False # to prevent opening browser window automatically when jupyter starts
    c.NotebookApp.password = u’sha1:YOUR_PASSWORH_HASH’
    c.NotebookApp.port = 8888
    c.NotebookApp.allow_origin = ‘*’ # allow all sources to connect

    Save and exit by pressing ctrl+x and Y and Enter

  9. Copy your notebook file on the Notebook folder you created earlier. I just pushed to github from my local machine and cloned into AWS machine.

  10. Now you’re ready to connect to your server using you password. Just start the Jupyter server.

    screen
    jupyter notebook

    I prefer to use a screen so that I can do other things on the same computer while Jupyter server session is running on my screen. If you want to get out of the screen, just do ctrl+a and then d. If you want to go back to the screen, use screen -ls and screen -r with the screen you want to connect to.

  11. Connect to your Jupyter notebook on AWS EC2 by copying your public IP from AWS EC2 console and adding post 8888 at the end.

    For example, if your IP is 54.12.12.12 then go to 54.12.12.12:8888. You will get a Jupyter screen asking for password. Provide the password that you used to create that SHA1 hash. And viola, you’re in. You can share that URL and password with your colleagues now.

    Remember to shutdown your EC2 when you no longer need it, and yes, I tested this on a free tier eligible machine.

comments powered by Disqus