DEV Community

loading...

A Simple AWS Pricing Ruby Client

bensign profile image Ben Schaechter ・2 min read

AWS pricing ranks high on the list of complicated topics that developers need to invest time in understanding. Depending on the service, pricing can be done on multiple dimensions that oftentimes results in having to unpack complicated AWS bills on a monthly basis.

One website that has gained popularity over the years is ec2instances.info for how it simplifies down the user-experience around EC2 pricing. The data behind this website has just been made available through the recent launch of the Vantage API and available as an official Ruby client library.

The API is completely free to use and makes getting EC2 instance product and pricing details pretty easy. A few examples are below.

Getting EC2 Product Details:

Getting EC2 instance product information is one of the most used functions of the API. Here's an example of how to get product information for a c5.xlarge EC2 instance:


# Load the gem
require 'vantage-client'

# Configure Vantage with your API token. Replace "YOUR_API_TOKEN" with the token you generate. 
Vantage.configure do |config|
  config.access_token = 'YOUR_API_TOKEN'
end

# The Vantage::PricesApi class helps get simple information 
vantage_client = Vantage::PricesApi.new

# Find all instances with the instance type name of 'c5.xlarge'
response = vantage_client.get_products(name: 'c5.xlarge')

# See the entire response
puts response.products.first
# { 
#   :id=>"aws-ec2-c5_xlarge", 
#   :category=>"compute", 
#   :name=>"c5.xlarge", 
#   :service_id=>"aws-ec2", 
#   :provider_id=>"aws", 
#   :details=>{
#     :gpu=>0, 
#     :name=>"C5 High-CPU Extra Large", 
#     :vcpu=>4, 
#     :memory=>8.0, 
#     :storage=>nil, 
#     :clock_speed_ghz=>3.0, 
#     :physical_processor_description=>"Intel Xeon Platinum 8124M", 
#     :network_performance_description=>"Up to 10 Gigabit"
#   }
# }

Enter fullscreen mode Exit fullscreen mode

Getting EC2 Instance Pricing

Getting the price of an EC2 instance is also easy to do. Here's an example of how to get the price for a c5.xlarge. You'll need the product ID from the prior call to retrive prices. Note that Vantage will return all prices across all regions, platforms and lifecycle so client-side you'll want to filter for the appropriate region, platform and lifecycle.

The price returned in the example below will give you the on-demand price for a linux EC2 instance in us-east-1.


# Load the gem
require 'vantage-client'

# Configure Vantage with your API token.  Replace "YOUR_API_TOKEN" with the token you generate. 
Vantage.configure do |config|
  config.access_token = 'YOUR_API_TOKEN'
end

# The Vantage::PricesApi class helps get simple information 
vantage_client = Vantage::PricesApi.new

# Find all prices using the product ID returned in the previous example
# Note that this will return all prices across all regions and platform types by default.
response = vantage_client.get_prices('aws-ec2-c5_xlarge')

puts response.prices.first
# {
#   :id=>"aws-ec2-c5_xlarge-us_east_1-on_demand-linux", 
#   :unit=>"hour", 
#   :region=>"us-east-1", 
#   :rate_type=>"compute", 
#   :currency=>"USD", 
#   :amount=>0.17, 
#   :details=>{
#     :platform=>"linux", 
#     :lifecycle=>"on-demand"
#   }
# }


Enter fullscreen mode Exit fullscreen mode

Conclusion

Hopefully this post gives you a quick overview of some of the things you can do through the Vantage API with their official Ruby client. It's all completely free so give it a try yourself: https://github.com/vantage-sh/vantage-ruby

Discussion (0)

pic
Editor guide