Created
January 13, 2012 21:46
-
-
Save iconara/1608874 to your computer and use it in GitHub Desktop.
EC2 Cost Per Hour
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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