Skip to content

Instantly share code, notes, and snippets.

@jakechen
Last active December 11, 2023 18:11
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save jakechen/faf0500132d46d83517004bbfedbe5de to your computer and use it in GitHub Desktop.
Save jakechen/faf0500132d46d83517004bbfedbe5de to your computer and use it in GitHub Desktop.
Creating and connecting to Jupyter Notebooks in AWS EC2

Introduction

This quick guide describes how to create a Jupyter Notebook in AWS EC2 then how to access it remotely using SSH tunneling. This method is preferred since you do not open any additional ports besides 22, requires little-to-no configuration, and is generally more straight-forward.

Pre-requisites

This current version assumes basic familiarity with cloud computing, AWS services, and Jupyter Notebook. Mostly because this version won't have images and won't dive too deep into each individual step.

Steps

Spin-up EC2 instance with "Deep Learning" AMI

  1. Log into EC2 console and click "Launch Instance" button.
  2. Inside "AWS Marketplace", select the "Deep Learning AMI" from AWS. I use this AMI because most of the stuff you'll need is installed already.
  3. Select instance type depending on your use case. The "Deep Learning AMI" has support for GPU-backed instance (e.g. g2 and p2) but it's not necessary to use these. Use the cheapest one for your workload (likely memory needed for your dataset).
  4. In most cases you can use default settings throughout the rest.
  5. Ensure the instance is assigned to a security group with SSH access.
  6. Launch your instance

Open SSH Tunnel and start Jupyter Notebook

  1. Tunnel into your instance by adding -D -L localhost:8888:localhost:8888 to your SSH connection string. This should look like ssh -i "key.pem" ec2-user@ec2-123-456-789-123.compute-1.amazonaws.com -D -L localhost:8888:localhost:8888 .
  2. Start Jupyter Notebook in the background.
    1. We recommend using something like screen, tmux, or nohup to keep your notebook in the background even if you lose connection to your instance. Otherwise you may lose your in-progress models.
    2. Launch Jupyter Notebook using jupyter notebook --no-browser. The --no-browser flag prevents the server from launching a browser.

Tunnel into Notebook

  1. Open up any web browser and point it to http://localhost:8888 .

Conclusion

That's it! Remember to spin-down your EC2 instance after you're done with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment