Skip to content

Instantly share code, notes, and snippets.

@dalehamel
Last active August 29, 2015 14:22
Show Gist options
  • Save dalehamel/77188920788b02f1a7f0 to your computer and use it in GitHub Desktop.
Save dalehamel/77188920788b02f1a7f0 to your computer and use it in GitHub Desktop.
spotscrape
#!/usr/bin/env ruby
require 'aws-sdk'
require 'json'
$stdout.sync = true
conf = JSON.load(File.read(ENV['CONFIG']))['aws']
credentials = Aws::Credentials.new(conf['access_key_id'], conf['secret_access_key'])
ec2 = Aws::EC2::Client.new(region: 'us-east-1', credentials: credentials)
ec2.describe_regions.regions.each do |region|
puts region
ec2 = Aws::EC2::Client.new(region: region.region_name, credentials: credentials)
token = "foo"
region_data = []
data = ec2.describe_spot_price_history()
fname = "data/#{region.region_name}_#{Time.now.getutc.to_i}"
until token.empty?
last = data.spot_price_history.last
region_data += data.spot_price_history
token = data['next_token']
puts "#{last.availability_zone}:#{last.timestamp}:#{token}:#{token.empty?}:#{token.nil?}"
data = ec2.describe_spot_price_history(next_token: token)
File.open(fname, 'w+') do |file|
existing = JSON.load(file.read) || []
file.write(JSON.dump(existing + region_data))
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment