Skip to content

Instantly share code, notes, and snippets.

@copernicus
Forked from iconara/ebs_cost_per_month.rb
Created February 6, 2013 13:06
Show Gist options
  • Save copernicus/4722400 to your computer and use it in GitHub Desktop.
Save copernicus/4722400 to your computer and use it in GitHub Desktop.
# gem install aws-sdk
require 'aws'
PRICE_PER_GB_MONTH = 0.11
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_month = AWS.memoize do
ec2.volumes.reduce(0) { |sum, volume| sum + volume.size } * PRICE_PER_GB_MONTH
end
puts sprintf('$%.2f/month', cost_per_month)
# 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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment