First, install the dependencies. This is for Ubuntu Server 14.04.
Create a new file (e.g. launch-spot.py) and give it exec permissions (chmod +x launch-spot.py).
The script is less than 200 lines long and should be readable from top to bottom.
Set your Amazon AWS access keys as environment variables.
Or you can read the boto documentation to find out how to store the keys in configuration files.
Waiting for launch . . . . . .
Waiting for instance to boot . . . .
-n stands for --name and it is how you can identify this spot instance.
The profiles are hard coded in the script. Feel free to modify the script to load them from an external configuration file (in JSON perhaps).
You can run the script again and as long as you use the same name it will resume.
To run a script on this instance via SSH use one or more -s arguments.
Connecting to SSH [54.74.149.116]  .
You can copy & paste the last line in your terminal and connect to the instance.
As you can see, if you haven't specified key_pair in the profile, a new key pair will be created with the name KP-test and it will be saved as KP-test.pem.
If there's no security_group in the profile, a new one will be created with the name SG-test and the rules from the firewall profile setting will be applied. At a minimum, SSH from your IP should be allowed.
If you want to stop the spot instance, simply run sudo halt on the instance. The instance, the instance request and disks (if auto terminate is on) will automatically be shut down and terminated.
If you use a simple bash script, I suggest using one like this. set -e will stop the execution if an error occurs. If the script finished successfully, it will not be run again.
If you share a provisioning script with Vagrant and AWS, you can use this line to check if you are running on AWS EC2 or locally on Vagrant.
Amazon Ubuntu mirrors and apt-get can be slow and flaky. Especially if you hit CTRL+C while the script is executing apt-get and then run it again.
Create a file system on the instance disk and move /home, /tmp and /swapfile to it. This can be useful on large SSD backed instance disks.
To upload/download files to S3 on AWS and use local files in /vagrant when running in Vagrant, you could do something like this.