Skip to content

Instantly share code, notes, and snippets.

@dukejones
Created October 30, 2016 03:45
Show Gist options
  • Save dukejones/417771149c6dc092f0ed82a04318f85f to your computer and use it in GitHub Desktop.
Save dukejones/417771149c6dc092f0ed82a04318f85f to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'aws-sdk'
require 'pry-byebug'
regions = [
'us-west-2', # Oregon
'us-east-1', # N Virginia
'eu-west-1', # Ireland
'eu-central-1', # Frankfurt
'ap-northeast-1', # Tokyo
'ap-northeast-2', # Seoul
'ap-southeast-1', # Singapore
'ap-southeast-2', # Sydney
'ap-south-1', # Mumbai
'sa-east-1' # Sao Paolo
]
def spot_price_data(spot_price_history)
totals = spot_price_history.each_with_object({}) do |spot_price, totals|
totals[spot_price.instance_type] ||= {num: 0, prices: []}
totals[spot_price.instance_type][:num] += 1
totals[spot_price.instance_type][:prices] << spot_price.spot_price.to_f
end
data = totals.map do |instance_type, info_hash|
[
instance_type,
{
avg: info_hash[:prices].inject(0, :+) / info_hash[:num],
min: info_hash[:prices].min
}
]
end.to_h
end
instance_types = ["d2.8xlarge", "m4.8xlarge"]
all_data = regions.map do |region|
client = Aws::EC2::Client.new(region: region)
resp = client.describe_spot_price_history({
start_time: Time.parse("2016-10-28"),
# end_time: Time.parse("2016-10-29"),
instance_types: instance_types,
product_descriptions: [
"Linux/UNIX (Amazon VPC)",
],
})
data = spot_price_data(resp.spot_price_history)
{region => data}
end
puts all_data.to_json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment