Compare to on-demand instance which costs about $0.02/h, spot instance price starts from $0.006, almost a third of the other one. Running a micro instance for a month will be only $4.5.
Why so cheap? One possible reason would be, on-demand instances are charged by hour, which means even if you only used it for one minute then stop your instance immediately, you will still be charged for the full hour.
The only drawback for spot instance is that there is no guarantee for server instance to continue running. When market price goes up, spot instance will be terminated. Try to pick a quiet region if you can, e.g., us-east-1b price history seems very stable 0.006/h for a month, even for past 3 months ( only 2 spikes on Apr. 23)
When instance terminated, all data will be lost. Root ebs will be deleted automatically. If state data is needed, an extra ebs can be attached as user-data storage.There are many different posts showing how to attach another ebs during start-up, like coding in rc.d
but the easiest way is to set it in request, ec2 tool kit only.
ec2-request-spot-instances -p 0.006 -t t1.micro -n 1 -b ‘/dev/sdf=snap-a023dfdf::false’ ami-746fc91d -z us-east-1b -k mykeypaire -g my-group -r persistent
- Can not use sda1 （root), otherwise error: snapshotId cannot be modified on root device
- /dev/sdx start from a letter >f, in the example it uses b, I encountered an error saying snap id can only be changed on ebs device.
- Don’t forget the key-value pair and security group. They are key to access.
- -r persistent means repeat request.
Setup OS (environment, tools, your app, etc)
Spot instance should start working for your order immediately after request fulfilled, the perfect solution would be start from your private AMI.
To create an AMI is to request a on-demand instance, setup everything you need, then
- Option A: created from an existing instance, better to stop the instance before
- Option B: create an ebs snapshot, from which create a new image.
Write down your image id. then request spot instance. Request can be done through AWS console or ec2 tool kit.
Setup ec2 tool kit
Download zip file from http://developer.amazonwebservices.com/connect/entry.jspa?externalID=368&categoryID=88
Generate X509 cert following http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/using-credentials.html#using-credentials-certificate
upload those 2 files to your work machine under ~/.ec2 folder
$ export EC2_PRIVATE_KEY=~/.ec2/pk-blahblah.pem
$ export EC2_CERT=~/.ec2/cert-blahblah.pem
Test setup using a command like ec2-describe-regions
It should work.
Those export and path setup can be put into ~/.bash_profile or ~/.bashrc
Manually attach ebs
Created a new ebs 1G
Attach to new instance manually in AWS console
device name will change to /dev/xvdf from /dev/sdf in ubuntu
ebs needs format before mount
sudo mkfs -t ext3 /dev/xvdf
sudo mount -t ext4 /dev/xvdf /home/ubuntu/folder_to_mount
Set trust scp/ssh for rc2 instance on your own VPS
local: ssh-keygen -t dsa -f $HOME/.ssh/id_dsa -P ”
remote:(~/.ssh) cat id_dsa >> authorized_keys
Bad news, spot instance is not included in free tier.
All the saving cost export I spent so far seems useless, and I will revisit this post when my 12 month free tier expired.