Configure Amazon EC2 to Run PostgreSQL, Anaconda and Jupyter Notebook

Yamen Shabankabakibou
Analytics Vidhya
Published in
5 min readAug 8, 2020

In this tutorial, we are creating a new Amazon EC2 instance and installing Anaconda, Python, PostgreSQL, and Jupyter notebook to run our python scripts from our local machine(windows 10) on EC2 instance throw SSH.

1- Lunch EC2 Instance:

  • From AWS Management Console we chose EC2 service then we start by launching our new EC2 instance with an Ubuntu Server AMI.
  • For the sake of this tutorial, we will stay at the free tier of AWS by selecting instance type t2.micro which is free tier eligible.
  • We will leave the configuration details as it is, raise the volume size(optional step max 30GiB), we will proceed with no tags and finally, we will open our instance to SSH on port 22, PostgreSQL on port 5432, Jupiter notebook on port 8888, HTTP on port 80, and HTTPS on port 443 to our local machine IP only.
  • After verifying our configurations we attach a new key-pair to the instance or chose an existing one.

2- Configure SSH on Your Local Machine:

  • Since our local machine’s OS is probably windows 10🙏 SSH is provided with the CMD by default otherwise we will be needing to use puTTY to connect to the instance using throw SSH.
  • On our local machine, we navigate to C:\Users\our-local-user then .\ssh we add a new text file named ‘config’ then we fill it as shown in the picture below.

(note: to save the file with no extension we File → Save as → Write the file name inside double quotes, everything after the # sympol is only for domenstrations )

3- Connect Throw SSH to the EC2 Instance:

Open the CMD on your local machine or use (Windows Terminal), then type ‘ssh myaws’(the name we specified in the config file), you will be asked if you are sure you want to connect to the instance type ‘yes’.
Voila, now you are connected to the instance 👌.

4- Installing Anaconda on the EC2 Instance:

  • Go to Anaconda → on the bottom of the page Linux part right click on 64-Bit (x86) Installer (550 MB) → chose copy link address then in the CMD where you are connected to the instance write(or use the link impeded with the article but in the time you read this article newer version may have been introduced):
wget https://repo.anaconda.com/archive/Anaconda3-5.3.0-Linux-x86_64.sh
  • Use ‘ls’ command to find out which file have you installed then run it with the ‘sh’ command for me it was like this:
sh Anaconda3–2020.07-Linux-x86_64.sh
  • Accept the license agreement, accept the default install location, and say “yes” when it asks if you want to run conda init.
  • Run ‘source .bashrc’ to ensure the path to python is configured, now you are in the base Anaconda environment.
  • Run ‘conda deactivate’ to exit it.

5- Install and Configure PostgreSQL on the EC2 Instance:

  • To install PostgreSQL on your instance run:
sudo apt-get install postgresql postgresql-contrib
  • Set up your user ubuntu on PostgreSQL on AWS using the following commands:
sudo -u postgres createuser --superuser ubuntu
createdb
  • Run ‘psql’ command to check installation then ‘\q’ to exit.
  • To configure PostgreSQL run the below commands and search for #listen_addresses=’localhost’ and change it to listen_addresses=’*’ then save and exit:

sudo su -postgres
nano /etc/postgresql/10/main/postgresql.conf

  • We are going to edit another config file. In your terminal run the command below
nano /etc/postgresql/10/main/pg_hba.conf

and Search for “IPv4 local connections”. You should have a line allowing 127.0.0.1/32 to connect. Underneath this line, add the following:

host    all             all             65.209.60.146/0         trust
  • Finally, we exit the Postgres users with ‘exit’ command and we restart Postgres server using:
sudo service postgresql restart

6- Create a New Anaconda Environment on the EC2 Instance:

  • We create a new Anaconda environment with Psycopg2, Python version 3.7.7, and Anaconda packages(the order is important) then we activate it with the following commands:
conda create --name newenv Psycopg2 python=3.7.7 anaconda
conda activate newenv

7- Configure Jupyter on AWS EC2:

  • The first time we set up a machine, we need to configure Jupyter to allow remote connections and configure a password for Jupiter. on our instance we run:
jupyter notebook --generate-config
jupyter notebook password

8- Running Jupyter on the EC2 and Create the Tunnel from Your Local Machine:

  • We run the Jupyter notebook on our instance using the following command and we specify that there is no browser on our instance with no-browser flag:
jupyter notebook --no-browser
  • We open another CMD or (Windows Terminal) and create a tunnel between our local machine and the EC2 instance using the following command:
ssh myaws -L 8212:localhost:8888

where 8121 is the port number in which we want to open Jupyter notebook in our local machine and localhost:8888 is out Jupyter notebook address on our instance.

9- Open Jupyter Notebook Using the Local Machine Browser:

  • Using a web browser of your choice navigate to localhost:8212 address, you should see Jupyter asking for your entered password enter it and start coding 😎.

--

--