Skip to content

Instantly share code, notes, and snippets.

@arfon
Created January 1, 2013 00:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arfon/4424167 to your computer and use it in GitHub Desktop.
Save arfon/4424167 to your computer and use it in GitHub Desktop.
Retrieve and persist pricing histories for AWS spot
require 'aws-sdk'
require 'pry'
require 'mongoid'
Mongoid.configure do |config|
config.sessions = {
:default => {
:hosts => ["somewhere.mongohq.com:27083"], :database => "development", :username => "username", :password => "password" }
}
end
class InstanceType
include Mongoid::Document
field :name, type: String
field :availability_zone, type: String
field :product_description, type: String
field :pricing_history, type: Hash, :default => {}
index({ pricing_history: 1 }, { name: "pricing_index" })
end
InstanceType.delete_all
AMAZON_ACCESS_KEY_ID="ACCESS_KEY"
AMAZON_SECRET_ACCESS_KEY="SECRET_KEY"
AWS.config({
:access_key_id => AMAZON_ACCESS_KEY_ID,
:secret_access_key => AMAZON_SECRET_ACCESS_KEY
})
ec2 = AWS::EC2.new
ec2.client
count = 0
loop do
count += 1
break if count > 240
incoming = ec2.client.describe_spot_price_history
incoming[:spot_price_history_set].each do |pricing|
instance = InstanceType.find_or_create_by(:name => pricing[:instance_type],
:availability_zone => pricing[:availability_zone],
:product_description => pricing[:product_description])
instance.pricing_history["#{pricing[:timestamp]}"] = pricing[:spot_price]
instance.save
end
sleep 60
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment