Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 25th, 2012  |  syntax: None  |  size: 0.91 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # gem install aws-sdk
  2.  
  3. require 'aws'
  4.  
  5.  
  6. PRICES = {
  7.   'm1.small' => 0.095,
  8.   'm1.large' => 0.38,
  9.   'm1.xlarge' => 0.76,
  10.   't1.micro' => 0.025,
  11.   'm2.xlarge' => 0.57,
  12.   'm2.2xlarge' => 1.14,
  13.   'm2.4xlarge' => 2.28,
  14.   'c1.medium' => 0.19,
  15.   'c1.xlarge' => 0.76
  16. }
  17.  
  18. ACCESS_KEY = '...'
  19. SECRET_KEY = '...'
  20. ENDPOINT = 'eu-west-1.ec2.amazonaws.com'
  21.  
  22. ec2 = AWS::EC2.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_KEY, :ec2_endpoint => ENDPOINT)
  23.  
  24. cost_per_hour_by_type = AWS.memoize do
  25.   running_instances = ec2.instances.select { |i| i.status == :running }
  26.   running_instances.reduce(Hash.new(0)) { |acc, i| acc[i.instance_type] += PRICES[i.instance_type]; acc }
  27. end
  28.  
  29. total_cost = 0
  30.  
  31. cost_per_hour_by_type.keys.sort.each do |instance_type|
  32.   cost = cost_per_hour_by_type[instance_type]
  33.   total_cost += cost
  34.   puts(sprintf('%-10.10s %5.2f', instance_type, cost))
  35. end
  36.  
  37. puts(sprintf('%-10.10s %5.2f', 'total', total_cost))