Deep learning AWS EC2 set up
This is an accompanying blogpost which goes along with a video I’ve made about my AWS EC2 Deep Learning setup. The setup goes through the process of installing git, tmux, htop, and security certificates on an EC2 deep learning image. The bash scripts mentioned in the video can be found on the ml-setup repo.
sudo yum update -y
sudo yum install -y tmux htop
sudo openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout "cert.key" -out "cert.pem" -batch
from IPython.lib import passwd
Enter in the password which will be used to authenticate the Jupyter Notebook on the website. Paste this SHA string in some note-taking app, you’ll need it in a bit.
Note: The fastest way to jump to the bottom of a file in vim is
<ESC>GA according to this question.
Paste in the following code, and replace the very last line with the SHA string you had copied in the previous section.
c = get_config() # Get the config object. c.NotebookApp.certfile = u'/home/ec2-user/ssl/cert.pem' c.NotebookApp.keyfile = u'/home/ec2-user/ssl/cert.key' c.IPKernelApp.pylab = 'inline' # in-line figure when using Matplotlib c.NotebookApp.ip = '*' # Serve notebooks locally. c.NotebookApp.open_browser = False c.NotebookApp.password = 'sha1:fc216:3a35a98ed980b9...'
ssh-keygen -t rsa -b 4096 -C “email@example.com”
It is important to note that the “passphrase” that the
ssh-keygen command asks you to enter is NOT your github.com password. It’s simply a passphrase which will be accepted when you use this SSH method to authenticate (push / pull) from Github.
Go to account settings > SSH Keys > New SSH key
Paste in the
id_rsa.pub public key content
Create a new public or private Github repo where your notebooks and models will reside.
Test it out:
After running the following commands, you should be able to visit your Jupyter Notebook at this URL: https://localhost:8157
source activate python3
Create a AWS AMI (Image)
Go to the EC2 instance that you are running and go to Actions > Image > Create Image
Your image should appear under Images > AMI
After this happens, go ahead back to instances and terminate your running instance
On the instance:
After requesting a new spot instance which utalizes your newly created AMI, run
ssh.sh to connect to it. Bellow are the tmux commands I use in the video, but you can also refer to this handy tmux cheat sheet
tmux new -s ML
Ctrl+B % - split window vertically
Ctrl+B " - split window horizontally
Ctrl+B <arrow keys> - navigate between windows
Ctrl+B d - detach (exit) from tmux
tmux ls - list tmux sessions
tmux a -t ML - dive back into tmux
I should note, even if you detach from tmux, all the commands (such as
jupyter notebook and
htop) will continue to run. Refer to this helpful YouTube video about the power of tmux.
Conclusion (DO read this):
After you’re done doing your work, make sure to go into your AWS EC2 dashboard, and terminate any running instance. This can also be done using the AWS CLI:
aws ec2 terminate-instances --instance-ids <insert instance id>
Now anytime you want to get back into your Deep Learning setup, simply run the
spot-request.sh and you should pick up where you left of. Do note that the EC2 spot instances are ephemeral, and they will be terminated in the off chance that someone out-bids you. So get into the habit of frequently committing your Jupyter Notebooks / datasets / ML models into github.