Boost your Colab Notebooks with GCP and AWS Instance in a few minutes
Tags: tutorial, beginners, productivity, machinelearning
Published: 2020-08-28 07:56:31

For Data Scientists, Notebooks have become the 'de facto' tool while working on a project. Whether they perform EDA on the initially available dataset or begin with some data preprocessing steps or experiment with different models and libraries, notebook is the one they begin with. In this respect, [Colab Notebooks](https://colab.research.google.com/) are second to none - Easily available with almost all the libraries pre-installed, efficient enough with readily available GPUs and TPUs, can be saved on multiple locations from Drive to GitHub and also shared live with colleagues.

But sometimes you might require resources more than Colab typically offers, for example, you might require **multi-GPUs** or **higher GPU RAM** or maybe **a better GPU** or **more than 12 hours of runtime** (default) to conclude a successful DS experiments in your notebook. In this blog, I shall cover how to upgrade your Colab in a few minutes, receiving all the above mentioned benefits, without moving your code elsewhere, having Google Cloud Platform or Amazon Web Service Instance as its backend.

# Google Cloud Platform

When you first sign up on GCP, you will have $300 free credits. 

### Request an increase in GPU Quota

Your account typically does not come with GPU quota. You have to explicitly request for it under **IAM Admin > Quotas**.
You should change your quota of **GPU (all regions)**. Filter the Metric to GPU (all regions), increase its limit

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/rsctfqaele0juk6oibhu.png)

And Submit a request. Wait until GCP sends you a second email (the first email is just to notify you that they received the request). It would take a couple of minutes (or hours maybe) for them to approve.

## Create an Instance of your choice

Now go to **Compute Engine > VM instances** and create an instance

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6cb5yac70wfbig2ykz8w.png)

Fill the details as shown below:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ghlh199obqc2ff80ohk0.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/cndwpyn002epqcjbhyg3.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/bh9sqimgkgmcgfemxtix.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/urgqzgqw8mlslw3oxjiy.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/yitkwbmjvkq876ffbcef.png)

And click **Create**. 

Also, do this.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/tdaevvcdk969z7on16gx.png)

Once your instance is up, it shall display similar to this

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/84fvgfl0apt1i46o641k.png)


# Amazon Web Services

If you are using AWS for the first time, you can apply for receiving $300 free credits. 

### Request an increase in GPU Quota

Your account typically does not come with GPU quota. You have to explicitly request for it under **Support > Create Case > Service Limit Increase**. You should increase EC2 All P instances in your region by limit 1. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/v4ejanx8rpq1up7e1kpq.png)

Write a short description of the use case and submit a request. Wait until AWS replies to you back. It would take a couple of minutes (or hours maybe) for them to approve.

## Create an Instance of your choice

Now go to **EC2 > Launch instance** and create an instance

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/hyqhfkhjr9knmfm1azzf.png)

Follow the steps as shown below:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0mknlmpru643r5makenl.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/58vztaz7ya5sd8gl3eww.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/96or3m81rkktaq9zzaly.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7wqgztspctcsrfugg69y.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6covf3nruy8lih6rwft9.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/m233p45raxn2b8p7046d.png)

Download the key pair in your local storage (and maybe copy it into a secure location too). Tick the box and click **Launch Instances**. It shall show similar to this

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/q556ly9f34nh8mubcz2s.png)

Once your instance is up, it shall display similar to this

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/xl8q0m7hgbn8a74n34hv.png)


# Command Line Terminal and Colab

## For GCP:

Install [Google Cloud SDK using the quick start](https://cloud.google.com/sdk/docs/quickstarts) for your operating system.

Once Google cloud is set up, initialize GCloud SDK for your Google account: 

`gcloud init`

Now Connect to your server and forward your port:

`gcloud beta compute ssh --zone "us-central1-a" "colab-backend" --project "myfirstproject" -- -L 8888:localhost:8888`

## For AWS:

Go to the directory where your EC2 security key is located and run

`chmod 0400 awsec2keypair1.pem`

Next,

`ssh -L localhost:8888:localhost:8888 -i "awsec2keypair1.pem" ubuntu@ec2-33-142-118-69.compute-1.amazonaws.com`

## Common for both GCP & AWS:
First make sure this is installed: 

`pip install --upgrade jupyter_http_over_ws>=0.0.1a3 && \
  jupyter serverextension enable --py jupyter_http_over_ws`

**For AWS (not required for GCP):** Before launching notebook please activate the environment:

`source activate pytorch_p36`

you can check envs list by running `conda info --envs`

**Finally, launch your Jupyter notebook:**

`jupyter notebook \
  --NotebookApp.allow_origin='https://colab.research.google.com' \
  --port=8888 \
  --NotebookApp.port_retries=0`

It shall display similar to this:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/rso2rdksszq15ykcpwiz.png)

Copy the notebook link, go to Colab and follow these steps:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2gyni8dph5o3hpyqnqvb.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9s6jmpkw933xfzo7ib4x.png)

and click **Connect**.

To check if GCP/AWS backend is integrated:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/u12mibrdg8943pfq0r00.png)

Note: Google Colab currently doesn’t support integration with Google Drive while connected to a local runtime.

And Voilà! Your **SUPER-Colab** is ready!

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8c547ysatg5oe6r5gxyt.gif)


**Important: Don't forget to stop your GPU/AWS instance once you are done.** 

Let me know in the comments if you face any issue.