Howto: Automated AWS spot instance provisioning with persisting of data

The end result after completing all the steps in this howto


You need to be running any flavor of Linux. Most likely most of this will also work on Mac OS but I haven’t had a chance to try.

Bringing up the environment (Virtual Private Cloud)

  1. From your terminal, clone my scripts repository and cd into it
git clone && cd aws-setup

Configuring our workstation in the cloud

We will now spin up a brand new instance and configure it to our liking. We will then take an image of it and will use it as a blueprint for instances for continued use.

./ p2.xlarge 0.25
if [ -d “$HOME/aws_scripts” ] ; then
authorize-current-ip && spot-instance-connect
wget && bash
sudo shred -u /etc/ssh/*_key /etc/ssh/*
exec bash && ./start-jupyter-notebook
cat ~/aws_scripts/spot-instance-connect
https://<IP addr>:8888

Creating the persistent volume

We now need to create the volume that we will be attaching when we spin up a new spot instance and detaching before termination.

sudo fdisk /dev/xvdf
sudo mkfs -t ext4 /dev/xvdf1
cd workspace
sudo chmod go+rw .

Grande Finale

Most of the set up is complete. All we need to do now is create a network interface with a public IP and tag things appropriately so that our script can use them.

Workflow steps

Request the main-compute-instance from the cloned aws-setup repository

./ p2.xlarge 0.25
  1. Why do I need to authorize and deauthorize my IP address?
    This configures ingress rules in the security group of our VPC so that only computers with a given public IP address can access your notebook. This might be a nuisance to some and can be disabled from AWS console. Will provide more details if necessary.
  2. There are two additional steps that can make this setup even more convenient — I only configured this for myself several weeks after the initial version but I am enjoying those changes very much. This is what the process of booting up the instance looks like with additional configuration steps I mention below:
Starting the instance up after completing the additional configuration steps
  1. Configure /etc/fstab following the instructions described here starting with step #7.
  2. Add the following line to your /etc/rc.local
    su ubuntu -c ‘bash /home/ubuntu/start-jupyter-notebook
  3. Redo steps 7 through 9 of the above howto, then follow with step 26.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Radek Osmulski

Radek Osmulski


I ❤️ ML / DL ideas — I tweet about them / write about them / implement them. Recommender Systems at NVIDIA