0

I would like to be able to estimate the per hour cost of specific ec2 instances in my aws accounts using Aws::Pricing::Client or another module inside of the ruby aws-sdk gem.

However, I do find the documentation a bit confusing and could use some help in finding a good example on how to use this module. For example, how would I be able to use this to find the cost of an ec2 instance with a size of m3.medium, in us-east-1, with a 50 gb gp2 volume?

Better yet is there is a method I could use to give some aws-sdk module the instance id or name and receive an estimate of the per hour costs on that instance?

Alex Cohen
  • 5,596
  • 16
  • 54
  • 104
  • Side-note: The cost of EC2 is separate to the cost for EBS volumes. – John Rotenstein Feb 07 '19 at 00:41
  • did you look at https://docs.aws.amazon.com/sdkforruby/api/Aws/Budgets.html and https://aws.amazon.com/aws-cost-management/aws-budgets/ ? – lacostenycoder Feb 07 '19 at 03:57
  • you might be looking for https://docs.aws.amazon.com/sdkforruby/api/Aws/CostExplorer/Client.html#instance_method_details – lacostenycoder Feb 07 '19 at 04:22
  • @JohnRotenstein yes the cost of EC2 and EBS are separate, but it seemed that some aws-sdk cost estimation modules might be able to do both calculations at once. However it seems that I will likely require two separate modules to calculate the full cost . – Alex Cohen Feb 07 '19 at 18:15
  • @lacostenycoder I love the `Aws::CostExplorer` sugesstion! Unfourtunately it only seems to be available for `us-east-1` :( https://docs.aws.amazon.com/general/latest/gr/rande.html#billing_region – Alex Cohen Feb 07 '19 at 18:28
  • @AlexCohen I think that may be the endpoint where the services is hosted, but since `:region` is required option I think you might be mistaken. Did you give it a try? see https://docs.aws.amazon.com/sdkforruby/api/Aws/CostExplorer/Client.html#constructor_details and search for `:region` – lacostenycoder Feb 07 '19 at 18:38

1 Answers1

0

Unfortunately, it seems that there is no direct aws-sdk module that can be used to access ec2 and ebs costs across all regions. However, it seems that the AWS pricing API will be the closest solution providing costs for both resources across all aws regions.

More details on this api can be found in this post: get ec2 pricing programmatically?

The api produces jsonp files. To make this easily accessible in ruby I would then recommend taking a look at this post that uses examples direcly from the pricing API: Ruby : How to parse a jsonp and save json data to database

Fair warning, you will need to parse this heavily to be able to access any information. For example, this solution will provide a hash containing the pricing data for a t2.medium instance in us-west-2

#!/usr/bin/env ruby

require 'net/http'
require 'json'

ec2_linux_price = JSON.parse(
  Net::HTTP.get(
    URI.parse('http://a0.awsstatic.com/pricing/1/ec2/linux-od.min.js')
  ).split('callback(')[1].sub(');', '').gsub(/(\w+):/, '"\1":')
)

region_prices = ec2_linux_price["config"]["regions"].select{|region| 
  region["region"]=="us-west-2"
}[0]["instanceTypes"].map{|types| 
  types["sizes"]
}

cost_hash={}
region_prices.each do |size_range| 
  cost = size_range.select{|size| 
    size["size"]==instance[:instance_type]
  }[0]
  unless cost.nil?
    cost_hash["t2.medium"] = cost["valueColumns"][0]["prices"]["USD"]
  end
end

p cost_hash
Alex Cohen
  • 5,596
  • 16
  • 54
  • 104