Connecting Jupyter Notebook to AWS EC2 Instance
Data scientists & machine learning engineers require increasing computational power to process data sets and train their machine learning models. The issue of low computational power can be easily avoided by using custom-configured remote servers.
Jupyter Notebook is an easy-to-use interactive IDE equipped with presentation and visualization tool allowing you to deploy visualizations in the same notebook. It also supports markdowns which helps a lot in documentation.
On the other hand, Amazon Web Services (AWS) provides highly scalable remote servers which can be upgraded or downgraded seamlessly according to user requirements.
Here in this article, I’ll cover a step by step guide to connecting Jupyter Notebook with EC2 Instance of Amazon Web Services.
The process is short & quick and it is divided into three parts:
1. Launching an AWS EC2 Instance
2. Setting up Python and Jupyter Notebook
3. Port Forwarding
1. Launching an AWS EC2 Instance
- Create a new account or log in to an existing account on https://aws.amazon.com
- After logging in; click on the ‘Services’ and then on ‘EC2’
- Go to ‘Launch instance’
- Select ‘Amazon Machine Image (AMI)’ based on your preference. AMIs are remote servers and I have chosen ‘Ubuntu Server 18/04 LTS (HVM)’ for this post
- Select ‘Choose an Instance Type’ based on your preference. AWS is highly scalable and can be upgraded or downgraded whenever needed. I have chosen ‘t2.micro’ which is a free tier instance for this post
- Keep the default settings for ‘Configure Instance Details’, ‘Add Storage’ and ‘Add Tags’
- Now ‘Create a new security group’ in ‘Configure Security Group’
- After configuring the security group, click on ‘Review and Launch’
Tip: Select ‘My IP’ in source. Selecting it will add a layer of security to your remote server. The server will then only be accessed by your IP. ✅
- Review the instance and click on ‘Launch’
- If you are new user, you have to create a new key pair and download it to your system. This key will be required to connect to remote server
- If you have a key pair, click on ‘Choose an existing key pair’
- And finally, click on ‘Launch Instance’
- Your instance is now launching 🔥
Go to ‘Instances’ in EC2, and then on the newly created instance. Click on the connect button and copy given command. (Don’t forget to write the correct location of your .pem file in the command.)
Now we have launched an AWS EC2 instance😎. Next step is to set up Python and Jupyter Notebook in the remote server.
2. Setting up Python and Jupyter Notebook
- Open the terminal (use PowerShell in case of Windows)
- Run the command copied from Step 8 in the terminal
- Create a temporary directory in your remote server to download Anaconda Linux Installer
mkdir softwares
cd softwareswget https://repo.anaconda.com/archive/Anaconda3-2019.07-Linux-x86_64.sh (This will download Anaconda Installer in 'softwares' directory)
- Install Anaconda by using the following command
bash Anaconda3-2019.07-Linux-x86_64.sh
- Check if Python is successfully installed or not by entering ‘python3’ in the terminal
Python 3.6.8 (default, Jan 14 2019, 11:02:34)
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type “help”, “copyright”, “credits” or “license” for more information.
- Before working with Jupyter Notebook, you have to install ‘jupyter’ in your remote server with the help of the following command
sudo snap install jupyter
- Now set a password to Jupyter Notebook
jupyter notebook --generate-config
jupyter notebook password
- Everything is set now. Run Jupyter Notebook
jupyter notebook
Jupyter Notebook is now successfully running on your remote server.😎
3. Port Forwarding
Port Forwarding is a technique that redirects a request from one address and port number combination to another.
- Open a new terminal (use PowerShell in case of Windows)
- Run the following command in the terminal to forward port 8888 to your localhost port 8888
ssh -i "location of your .pem file" -N -f -L 8888:localhost:8888 ubuntu@IP_of_your_remote_server
- Now go to browser and enter ‘http://localhost:8888/’. Jupyter Notebook will be forwarded to your local host. Login and your Jupyter Notebook is now connected with your AWS EC2 Instance 🔥