Friday, April 2, 2010

EC2 Marks the Spot

I've been using Amazon EC2 at work and for personal projects for about three years now. It's got to the stage where I can't remember how we used to manage without being able to spin up a test or development machine on demand.

One downside of EC2 compared to other hosting options is the price. The cheapest on-demand Linux instance costs 8.5 cents per hour, which works out to just over $60 a month - a bit expensive if you just want to run something like a web server and don't need full sytem access. However, Amazon just came up with a way to cut this cost significantly by introducing Spot Instances.

You can read all the details by following the link, but the basic idea is that you bid on spare EC2 capacity by specifying the maximum price per hour that you're prepared to pay. If the current spot price (which varies continually based on supply and demand) is less than your bid, Amazon will start up your instance and keep it running until the spot price becomes higher than your bid. At that point your instance will be terminated.

The surprising thing I found after monitoring the small Linux instance spot price for a few days is that it's a LOT less than the on-demand price - it's stayed between 2.9c and 3.1c per hour. That means that if you bid, say, 5 cents per hour for your spot instance, you'll be pretty certain of getting a cheap, long-running instance unless there is a sudden spike in demand and the spot price goes over 5c / hour.

The only down side I've found so far, apart from the necessity to be prepared for the instance shutting down without warning (which you ought to do for all EC2 instances anyway) is that you seem to have to wait a while longer for your instance to be started up; the ones I tried took one or two hours, rather than the usual 15 minutes or so.

As a way to warn me if the EC2 spot price is getting close to my bid price, I wrote a Nagios plugin, check_ec2_spot_price, that will send me a warning if the spot price goes above a specified value. You can download it from the Nagios Exchange.

I'm also working on a Munin plugin that will graph the EC2 spot price over time; I'll post that here too when it's ready.


  1. If you could join a few dots you could detect that the spot price if getting close spin up an on-demand instance wait for it to become available and then shut-down the spot instance.

    When the price drops, you could have it automatically do the reverse.

    I think this means that Amazon are pulling a fast one. In a free market, the spot-price=on-demand price.

  2. How has this been going for you? Have your spot instances stayed valid with a large bid price so that they won't get out bidded?

  3. Kevin - it's been going pretty well. It's hard to predict the maximum price that the spot instances will spike up to so I've lost an instance or two, but on the whole I've saved a bundle of cash.

    The one down side I found is that if you start up a number of spot priced instances at once (say more than 5), it seems to drive the spot price up to your bid price. The lesson learned from that is to start your spot priced instances one by one.

  4. Interesting, thanks for the details! :)

  5. Hi,

    I'd like to monitor my spot prices automatically as I paid 3x more last month than I'd like to.

    How do I install this script I receive an error that says

    You must supply a warning value See --help for details

    I have python installed and am using an Amazon instance but have never heard of nagios.

    Thank you