Search code examples
amazon-web-servicesjupyter-notebookpython-poetryaws-cloud9

Running jupyter notebook on EC2 instance


I have a cloud 9 instance set up on my AWS account. I am using poetry as my package manager and have installed jupyter notebook.

When I run the command poetry run jupyter notebook it runs as it usually does, but when I press the links they say that the site can't be reached.

Some things I have noticed is that in some AWS documentation there is mention of using port 8080, I have tried that but it doesn't work.

I also see something about http vs https, so could it be that it won't display because the link is not https?

Regardless any help on how to open up a notebook running on the cloud 9 instance would be very helpful, thanks


Solution

  • If you want to access the notebook over HTTP:

    export PWD="MyPassword" # choose whatever password you want
    
    jupyter notebook --generate-config
    echo "conf = get_config()" >> ~/.jupyter/jupyter_notebook_config.py
    echo "conf.NotebookApp.ip = '0.0.0.0'" >> ~/.jupyter/jupyter_notebook_config.py
    echo "from IPython.lib import passwd" >> ~/.jupyter/jupyter_notebook_config.py
    echo "password = passwd('${PWD}')" >> ~/.jupyter/jupyter_notebook_config.py
    echo "c.NotebookApp.password = password"  >> ~/.jupyter/jupyter_notebook_config.py
    echo "conf.NotebookApp.port = 8888" >> ~/.jupyter/jupyter_notebook_config.py
    jupyter notebook
    

    then hit http://public-ip-address:8888 and login with the password you specified.


    If you want to access the notebook over HTTPS (with a self-signed certificate):

    export PWD="MyPassword" # choose whatever password you want
    
    export DNS=$(curl -s http://169.254.169.254/latest/meta-data/public-hostname)
    sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /home/ec2-user/mykey.key -out /home/ec2-user/mycert.pem -subj "/C=US/ST=Oregon/L=Portland/O=Company Name/OU=Org/CN=$DNS"
    jupyter notebook --generate-config
    echo "conf = get_config()" >> ~/.jupyter/jupyter_notebook_config.py
    echo "conf.NotebookApp.ip = '0.0.0.0'" >> ~/.jupyter/jupyter_notebook_config.py
    echo "from IPython.lib import passwd" >> ~/.jupyter/jupyter_notebook_config.py
    echo "password = passwd('${PWD}')" >> ~/.jupyter/jupyter_notebook_config.py
    echo "c.NotebookApp.password = password"  >> ~/.jupyter/jupyter_notebook_config.py
    echo "conf.NotebookApp.port = 8888" >> ~/.jupyter/jupyter_notebook_config.py
    jupyter notebook --certfile=/home/ec2-user/mycert.pem --keyfile /home/ec2-user/mykey.key
    

    then hit https://public-ip-address:8888 and login with the password you specified.


    In either case, make sure that port 8888 is allowed in the EC2 security group and network ACL.