How to set up your AWS EC2 instance and get Jupyter Notebook up and running

Alyssa Liguori
Analytics Vidhya
Published in
4 min readNov 15, 2019

Create an EC2 instance, install python3, pip, and python libraries, and get Jupyter Notebook running.

  1. Create an AWS Account or login to an existing account and navigate to the AWS Management Console.
AWS Management Console

2. Under All Services > Compute > Click EC2

3. Click Launch Instance

4. You now have 7 steps to creation of your instance.

Heads up, you will either need to create a public/private key pair or supply an existing key pair in order to launch your instance. From AWS’s EC2 Key-Pairs documentation — “At boot time, the public key content is placed on the instance in an entry within ~/.ssh/authorized_keys. To log in to your instance, you must specify the private key when you connect to the instance.”

> Step 1: Choose AMI. If you want to learn more, check this AMI User Guide documentation. To follow my code, use Amazon Linux.

> Step 2: Choose Instance Type. Read more about instance types in this documentation.

> Step 3: Configure Instance Details. Leave defaults in place unless your use case requires tweaks.

> Step 4: Add Storage. The free tier qualifies for up to 30 GB storage. If needed, change the default of 8 GB to 30.

> Step 5: Add Tags. This step is to help you stay organized. Add or leave off tags depending on your preference.

> Step 6: Configure Security Group. Read more about security groups with AWS to get a good understanding.

> Step 7: Review Instance Launch. Ensure all settings are as you like then click Launch.

Step 7: Review Instance Launch

5. Your instance has launched. Check the status by navigating to the EC2 Dashboard (Services > EC2) then click Instances. When you see running under Instance State, proceed to the next step! (You may need to horizontal scroll to see all of the details of your instance).

EC2 Dashboard to check Instance State

6. Next to the Launch Instance button, click Connect. Keep this screen handy, you will need it shortly.

7. Depending on your machine, either download PuTTY or use the terminal to initiate connection to the SSH (Secure Shell) server. You will need your private key here. Here’s a quick description of SSH. Refer to the Connect screen mentioned in Step 6. Assuming you have a pem file with your private key in it. Navigate to that directory then use the following code to change the permissions of your pem file then connect to the instance’s public DNS.

# Here's an example with a pem file named "my-pem-file" with a private keychmod 400 'my-pem-file.pem'# Your login is ec2-user if you chose the Amazon Linux AMI during instance creation. After the @ symbol, enter your instance's Public DNS (IPv4) found on the same page where you found the Instance State ssh -i 'my-pem-file.pem' ec2-user@insert-public-dns-here

8. Now that you’ve connected, check the python version on your instance

python --version
python3 --version

9. Amazon Linux only comes with python 2.7 so we will need to install an update. Here’s the command:

sudo yum install python3 

10. Install pip3

curl -O https://bootstrap.pypa.io/get-pip.py
python3 get-pip.py

11. Install desired python libraries using pip3 (numpy, pandas, jupyter, scikit-learn, matplotlib)

12. Open the .bashrc file with vim. Add alias python=python3 then :wq to save and quit vim. Double check it worked.

ls -la 
vim .bashrc
python --version # should now print out the python 3.7 instead of 2.7

13. Change your default jupyter kernel to python3 then check the spec list.

ipython3 kernelspec install-self
jupyter kernelspec list

14. To use Jupyter in your browser, go to EC2 Dashboard > Security Groups and edit the security group attached to your instance. Add inbound rule: For Type use Custom TCP Rule and set Port Range to 8888.

AWS Security Groups

15. Start your jupyter notebook server

jupyter notebook — ip=* — allow_origin=* 

16. Paste your public DNS (IPv4) into your favorite browser followed by :8888

17. You will see this screen. Go to your terminal to retrieve the token that printed after you ran…

jupyter notebook — ip=* — allow_origin=*
Page requesting token in the browser

and paste token into the token text box in your browser.

18. Congratulations! You’re ready to start working in your Jupyter Notebook on your new EC2 instance.

Please let me know what questions you have or if you need help troubleshooting by leaving a comment!

--

--