How to Run Jupyter In Any Cloud With One Command
Tags: jupyter, terraform, machinelearning, datascience
Published: 2022-04-28 16:31:02

Two pain points often faced by scientists and engineers are:

- Installing and maintaining your entire software stack (including GPU drivers and dependencies) is difficult and *time-consuming*, and
- Being *limited by the hardware you own* (laptop or desktop specs).

Here's a solution!

## Requirements

1. Download the [terraform](https://www.terraform.io/downloads) CLI tool and say goodbye to [ClickOps](https://www.august.com.au/blog/killing-click-ops-what-it-is-why-its-problematic-and-how-to-avoid-it/) (free)
2. Get an [ngrok](https://ngrok.com) account for port forwarding convenience (free)
3. Have cloud credentials, of course! ([AWS](ttps://registry.terraform.io/providers/iterative/iterative/latest/docs/guides/authentication#amazon-web-services), [Azure](ttps://registry.terraform.io/providers/iterative/iterative/latest/docs/guides/authentication#microsoft-azure), [GCP](ttps://registry.terraform.io/providers/iterative/iterative/latest/docs/guides/authentication#google-cloud-platform), or [K8s](ttps://registry.terraform.io/providers/iterative/iterative/latest/docs/guides/authentication#kubernetes).)

## Lift off! :rocket:

Using [this GitHub repository](https://github.com/iterative/blog-tpi-jupyter) and [TPI] to do all the heavy lifting:

[TPI]: https://github.com/iterative/terraform-provider-iterative

```bash
git clone https://github.com/iterative/blog-tpi-jupyter
cd blog-tpi-jupyter
export NGROK_TOKEN="..."  # Signup for free at https://ngrok.com
terraform init    # Setup local dependencies
terraform apply   # Create cloud resources & upload workdir
terraform refresh # Get URLs (rerun if blank)
```

It takes a few minutes to launch cloud resources and upload your working directory, so you might have to wait a bit before running `terraform refresh` again. Soon, you'll see:

```js
Outputs:
urls = [
  "Jupyter Lab: https://{id}.ngrok.io/lab?token=...",
  "Jupyter Notebook: https://{id}.ngrok.io/tree?token=...",
  "TensorBoard: https://{id}.ngrok.io",
]
```

Click on those URLs to get to your dev environment! :sparkles:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bv7olbk29gmybj8ny27a.png)

## Download & Cleanup :inbox_tray:

When done experimenting, download the shared `workdir`, delete the cloud storage, and terminate the cloud instance with one simple command:

```bash
terraform destroy
```

## Benefits :tophat:

It uses [Terraform Provider Iterative (TPI)][TPI] under-the-hood. There are a few distinct advantages to TPI:

💰 **Lower cost**: use your preferred cloud provider's existing pricing, including on-demand per-second billing and bulk discounts.

🔄 **Auto-recovery**: spot/preemptible instances are cheap but unreliable. TPI reliably and automatically respawns such interrupted instances, caching & restoring the working directory in the cloud even when you are offline.

👓 **Custom spec**: full control over hardware & software requirements via a single [`main.tf` config file](https://github.com/iterative/blog-tpi-jupyter/blob/aws/main.tf) - including machine [types](https://registry.terraform.io/providers/iterative/iterative/latest/docs/resources/task#machine-type) (CPU, GPU, RAM, storage) & [images](https://registry.terraform.io/providers/iterative/iterative/latest/docs/resources/task#machine-image).

Happy coding!