Provisioning AWS Ubuntu-based EC2 for Python Deep Learning Projects

Below are my notes for provisioning a Python3/Jupyter server using AWS’ Ubuntu AMI. These notes captured my installation process during the week of 25 November 2019.

My goal for this document is to list the various steps required to create a GPU-equipped Linux instance in AWS that can be used for machine/deep learning project in Python.

Key Abbreviations

  • AWS: Amazon Web Services
  • VPC: Virtual Private Cloud
  • EC2: Elastic Compute Cloud
  • IAM: Identity and Access Management
  • AMI: Amazon Machine Image

Requirements

I needed to find a workable configuration for modeling deep learning problems using a GPU-equipped Linux EC2 instance in AWS. The Linux instance needs to support Python 3, Jupyter Notebook/Lab, and TensorFlow 2.0.

AWS does offer deep learning AMIs that can support Python 3, Jupyter, and TensorFlow 1.x. I used this exercise to build a customized Ubuntu Linux instance with TensorFlow 2.0.

Background and Prerequisite Information

The following tools and assumptions were present before the provisioning of the cloud instance.

  • AWS Console with the necessary rights and configuration elements to launch an instance. I had configured a VPC subnet, an IAM role, a security group, and a key pair for setting up the instance.
  • Web browsers
  • Access to SSH

AMI: I performed the following steps using the Ubuntu Server 18.04 LTS x86 AMI (ami-04b9e92b5572fa0d1)

EC2: I used the GPU instance p2.xlarge with 4 CPUs and 61 GiB of memory.

VPC: This exercise requires only a subnet that is accessible via the Internet.

Security Group: I configured the security group to allow only TCP ports 22 from any IP address because I had planned to use an SSH tunnel to access the Jupyter server.

IAM Role: I assign all my AWS instances to an IAM role by default. For this exercise, an IAM role is not critical.

Key Pair: I attached the instance to an existing key pair. The key pair is necessary to access the instance via the SSH protocol.

Additional Reference: TensorFlow > Install > GPU Support (https://www.tensorflow.org/install/gpu)

Provisioning the AWS instance

Step 1) Create and launch the instance. Access the instance with the ssh command (with tunneling enabled for port 8888):

ssh -i ~/.ssh/da-ml-keypair.pem -L localhost:8888:localhost:8888 ubuntu@<instance IP>

Step 2) Update the Ubuntu installation.

sudo apt-get update

sudo apt-get upgrade -y

Step 3) Add NVIDIA package repositories.

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-repo-ubuntu1804_10.0.130-1_amd64.deb

sudo dpkg -i cuda-repo-ubuntu1804_10.0.130-1_amd64.deb

sudo apt-key adv –fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub

sudo apt-get update

wget http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64/nvidia-machine-learning-repo-ubuntu1804_1.0.0-1_amd64.deb

sudo apt install ./nvidia-machine-learning-repo-ubuntu1804_1.0.0-1_amd64.deb

sudo apt-get update

Step 4) Install the NVIDIA driver (version 440 as of this writing).

sudo apt-get install -y nvidia-driver-440

Step 5) Reboot the instance and ssh back into it. Run the nvidia-smi command to check that GPUs are visible.

Step 6) Install CUDA development and runtime libraries. (Large install with ~4GB of files)

sudo apt-get install -y cuda-10-0 libcudnn7 libcudnn7-dev

Step 7) Verify Python installation and install/upgrade PIP as necessary. TensorFlow 2 packages require a pip version >19.0.

sudo apt install -y python3-pip

sudo pip3 install –upgrade pip

Step 8) Install the necessary Python machine/deep learning modules. Some screenshots were skipped.

sudo pip3 install numpy scipy matplotlib ipython sympy pandas jupyterlab

sudo pip3 install PyMySQL imbalanced-learn xgboost scikit-learn statsmodels

sudo pip3 install seaborn pmdarima lxml html5lib requests beautifulsoup4

sudo pip3 install tensorflow tensorflow-gpu keras

Step 9) Start up iPython and verify the TensorFlow and GPU installations.

In [1]: import tensorflow as tf

In [2]: tf.__version__

In [3]: tf.config.experimental.list_physical_devices()

The physical device types of CPU and GPU (not just XLA_CPU and XLA_GPU) should appear.

Step 10) Start up the Jupyter Notebook/Lab server.

jupyter lab –no-browser

Step 11) Start a browser session to access the Jupyter Notebook/Lab server in AWS. The web browser should be run on the same workstation, where we started the ssh tunneling session.

There you have it! A working Jupyter server on an AWS cloud instance that you can access via a secured protocol. Now load up your favorite scripts and let them run.

Step 12) Shut down the un-used GPU instance to save money.

Provisioning AWS Linux AMI-based EC2 for R Server

Below are my notes for provisioning an R server using AWS’ Linux 2 AMI. These notes captured my installation process during the week of 23 July 2018.

My goal for this document is to list the various reference points where you can find the step-by-step setup instructions for this provisioning task. This post will also comment on the obstacles I had run into during the provisioning, and what I needed to do to get past those obstacles.

You can find the installation and configuration notes below or here on the website.

Abbreviations

  • AWS: Amazon Web Services
  • VPC: Virtual Private Cloud
  • EC2: Elastic Compute Cloud
  • IAM: Identity and Access Management
  • AMI: Amazon Machine Image
  • DLAMI-DG: Deep Learning AMI Developer Guide

Requirements

Needed to find workable configurations for modeling machine learning problems by exploring the use of AWS Ec2 instances. The source code language was in R and contained in an RMD script format.

The baseline performance was defined by running the Python script on a Dell Latitude E7450 with Intel i7-5600U CPU at 2.60GHz, 16GB RAM, and Windows 10. While this can be a decent configuration for ML modeling, occasionally we may need a system with larger memory capacity or more CPUs for the tasks at hand.

Background and Prerequisite Information

The following tools and assumptions were present prior to the provisioning of the cloud instance.

  • AWS Console with the necessary rights and configuration elements to launch an instance. I had configured a VPC subnet, an IAM role, a security group, and a key pair for setting up the instance.
  • AWS Deep Learning AMI Developer Guide, released June 6, 2018
  • Web browsers
  • PuTTY

AWS Configuration Notes

AMI: I performed the following steps using Amazon Linux 2 AMI with an m5.large general-purpose instance.

VPC: This exercise requires only a subnet that is accessible via the Internet.

Security Group: I configured the security group to allow only TCP ports 22 from any IP address because I had planned to use an SSH tunnel to access the R server.

IAM Role: I assign all my AWS instances to an IAM role by default. For this exercise, an IAM role is not critical.

Key Pair: I attached the instance to an existing key pair. The key pair is necessary to access the instance via the SSH protocol.

 

Provision an instance with the Amazon Deep Learning AMI

Step 1) Create and launch the instance. I used an m5.large instance as the starting point.

Step 2) Install R base package (R3.4 as of this writing).

$ sudo amazon-linux-extra install R3.4

Step 3) Install R Server with the following commands. Check www.rstudio.org for the latest release of the server.

$ wget https://download2.rstudio.org/rstudio-server-rhel-1.1.456-x86_64.rpm

$ sudo yum install rstudio-server-rhel-1.1.456-x86_64.rpm

Step 4) Configure the client workstation to connect to the R server. I configured my Windows workstation to connect to the R server using an SSH tunnel. The DLAMI-DG document has a write-up on how to do this for Windows, Linux, and MacOS clients (pages 15-20).

See the PuTTY screenshot below for configuring an SSH tunnel.

provision_aws_linux_R_server-1

Step 5) Install Git.

$ sudo yum install -y git

$ git clone https://github.com/daines-analytics/tabular-data-projects.git

Step 6) Add an user to access the R server.

$ sudo useradd rstudio

$ echo rstudio:rstudio | sudo chpasswd

Step 6) Start a browser on the workstation running the SSH tunnel and point to the URL http://localhost:8787. A login screen should appear.

provision_aws_linux_R_server-2

Step 7) Go to the Terminal tab and run the “git clone” commands to copy my R scripts from GitHub to the cloud server. Locate the R script and run it.

provision_aws_linux_R_server-3

There you have it! A working R server on an AWS cloud instance that you can access via a secured protocol. Now install your favorite packages and let the scripts run.

When compared to a client workstation, the right types of cloud instance can help our modeling effort. For anyone who will be attempting a similar installation, I hope these instructions can help in some way. My next step is to automate the instance creation with a CloudFormation script further. I will write down what I run into and share my findings later.

Provisioning AWS Deep Learning AMI-based EC2 for Jupyter

Below are my notes for provisioning a Jupyter server using AWS’ Deep Learning AMI. These notes captured my installation process during the week of 23 July 2018.

My goal for this document is to list the various reference points where you can find the step-by-step setup instructions for this provisioning task. This post will also comment on the obstacles I had run into during the provisioning, and what I needed to do to get past those obstacles.

You can find the installation and configuration notes below or here on the website.

Abbreviations

  • AWS: Amazon Web Services
  • VPC: Virtual Private Cloud
  • EC2: Elastic Compute Cloud
  • IAM: Identity and Access Management
  • AMI: Amazon Machine Image
  • DLAMI-DG: Deep Learning AMI Developer Guide

Requirements:

Needed to find workable configurations for modeling machine learning problems by exploring the use of AWS Ec2 instances. The source code language was in Python and contained in a Jupyter notebook.

The baseline performance was defined by running the Python script on a Dell Latitude E7450 with Intel i7-5600U CPU at 2.60GHz, 16GB RAM, and Windows 10. While this can be a decent configuration for ML modeling, occasionally we may need a system with larger memory capacity or more CPUs for the tasks at hand.

The performance of the end-to-end script processing time on cloud instance should be comparable or even better than the baseline workstation.

Background and Prerequisite Information

The following tools and assumptions were present prior to the provisioning of the cloud instance.

  • AWS Console with the necessary rights and configuration elements to launch an instance. I had configured a VPC subnet, an IAM role, a security group, and a key pair for setting up the instance.
  • AWS Deep Learning AMI Developer Guide, released June 6, 2018
  • Web browsers
  • PuTTY

AWS Configuration Notes

AMI: I performed the following steps using both the Ubuntu-based and the Amazon Linux-based AMIs with an m5.large general-purpose instance. The AMIs were designed to take advantage of instances with GPUs. I found no issue running either AMI without the GPU; however, some pre-supplied tutorial examples probably need to be tweaked before they would work on a general-purpose instance.

VPC: This exercise requires only a subnet that is accessible via the Internet.

Security Group: I configured the security group to allow only TCP ports 22 from any IP address because I had planned to use an SSH tunnel to access the Jupyter server.

IAM Role: I assign all my AWS instances to an IAM role by default. For this exercise, an IAM role is not critical.

Key Pair: I attached the instance to an existing key pair. The key pair is necessary to access the instance via the SSH protocol.

Provision an instance with the Amazon Deep Learning AMI

Step 1) Create and launch the instance. I used an m5.large instance as the starting point.

Step 2) Configure the client workstation to connect to the Jupyter server. I configured my Windows workstation to connect to the Jupyter server using an SSH tunnel. The DLAMI-DG document has a write-up on how to do this for Windows, Linux, and MacOS clients (pages 15-20).

See the PuTTY screenshot below for configuring an SSH tunnel.

provision_aws_deep_learning_ami-1

Step 3) Run the “git” commands to copy my Python scripts from GitHub to the cloud server.

$ git clone https://github.com/daines-analytics/sandbox-projects.git

Step 4) Activate the Python 3 environment by running the command:

$ source activate python3

Step 5) Because my Python script required numpy, pandas, scipy, scikit-learn, and matplotlib packages, I needed to install some additional packages.

On the Ubuntu AMI, I ran the command “conda install <package>” to check or to install them.

On the Amazon Linux AMI, I ran the command “pip install <package>” to check or to install them.

Step 6) Start the Jupyter server by running the command:

$ jupyter notebook

provision_aws_deep_learning_ami-2

Step 7) Make a note of the Jupyter server URL and use that on the workstation browser running the SSH tunnel.

provision_aws_deep_learning_ami-3

Step 8) Locate the Python script and run it (my own Git folder circled below).

provision_aws_deep_learning_ami-4

Step 9) Compare the run-time script lengths. Not rigidly scientific but probably good enough.

Windows Workstation: 1 hour 50 minutes

Ubuntu/Deep Learning AMI: 1 hour 22 minutes

Amazon Linux/Deep Learning AMI: 1 hour 24 minutes

There you have it! A working Jupyter server on an AWS cloud instance that you can access via a secured protocol.

When compared to a client workstation, the right types of cloud instance can help our modeling effort. For anyone who will be attempting a similar installation, I hope these instructions can help in some way. My next step is to automate the instance creation with a CloudFormation script further. I will write down what I run into and share my findings later.

Provision a LAMP Server in the AWS GovCloud Region

This is my notes for provisioning a LAMP server in the Amazon Web Services (AWS) GovCloud region using Amazon’s Linux AMI. I had to set up a LAMP server and secured it with HTTPS. These notes outlined my installation process during the week of 12 March 2018.

My goal for this document is to list the various reference points where you can find the step-by-step setup instructions for this provisioning task. This post will also comment on the obstacles I had run into during the provisioning, and what I needed to do get past those obstacles.

You can find the installation and configuration notes here on the website.