
Untitled
By: a guest on
Jun 25th, 2012 | syntax:
None | size: 0.91 KB | hits: 7 | expires: Never
# gem install aws-sdk
require 'aws'
PRICES = {
'm1.small' => 0.095,
'm1.large' => 0.38,
'm1.xlarge' => 0.76,
't1.micro' => 0.025,
'm2.xlarge' => 0.57,
'm2.2xlarge' => 1.14,
'm2.4xlarge' => 2.28,
'c1.medium' => 0.19,
'c1.xlarge' => 0.76
}
ACCESS_KEY = '...'
SECRET_KEY = '...'
ENDPOINT = 'eu-west-1.ec2.amazonaws.com'
ec2 = AWS::EC2.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_KEY, :ec2_endpoint => ENDPOINT)
cost_per_hour_by_type = AWS.memoize do
running_instances = ec2.instances.select { |i| i.status == :running }
running_instances.reduce(Hash.new(0)) { |acc, i| acc[i.instance_type] += PRICES[i.instance_type]; acc }
end
total_cost = 0
cost_per_hour_by_type.keys.sort.each do |instance_type|
cost = cost_per_hour_by_type[instance_type]
total_cost += cost
puts(sprintf('%-10.10s %5.2f', instance_type, cost))
end
puts(sprintf('%-10.10s %5.2f', 'total', total_cost))