0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

現在のインスタンス料金を取得する script: RDS 編

Last updated at Posted at 2017-08-21

http://qiita.com/bells17/items/5326d11edc6acc4feea2
の RDS 版です

注意点として取得するデータのエンジンを Aurora に絞ってます

rds.rb
require 'json'
require 'bigdecimal'

results = {}
json_data = open(ARGV[0]) {|io| JSON.load(io) }

# product 情報を取得
json_data['products'].keys.each do |skuNo|
	product = json_data['products'][skuNo]

	if (product['productFamily'] == 'Database Instance' and
		  product['attributes']['locationType'] == 'AWS Region' and
		  product['attributes']['location'] == 'Asia Pacific (Tokyo)' and
		  product['attributes']['databaseEngine'] == 'Aurora MySQL') # Aurora だけに絞ってます (エンジンが mysql か postgresql かは無いっぽい??)

		results[product['sku']] = {
			sku: product['sku'],
			location: product['attributes']['location'],
			instanceType: product['attributes']['instanceType'],
			instanceFamily: product['attributes']['instanceFamily'],
			vcpu: product['attributes']['vcpu'],
			physicalProcessor: product['attributes']['physicalProcessor'],
			clockSpeed: product['attributes']['clockSpeed'],
			memory: product['attributes']['memory'],
			networkPerformance: product['attributes']['networkPerformance'],
			currentGeneration: product['attributes']['currentGeneration'],
			price_unit: 'USD'
		}

	end
end


# price

# on demand
json_data['terms']['OnDemand'].keys.each do |skuNo|
	if (results[skuNo])
		results[skuNo][:price_per_hour] = Proc.new {
			skuTerm = json_data['terms']['OnDemand'][skuNo][json_data['terms']['OnDemand'][skuNo].keys[0]]
			priceInfo = skuTerm['priceDimensions'][skuTerm['priceDimensions'].keys[0]]
			sprintf('%.3f', BigDecimal(priceInfo['pricePerUnit']['USD']).floor(2).to_f.to_s)
		}.call
		results[skuNo][:price_per_day] = sprintf('%.3f', (BigDecimal(results[skuNo][:price_per_hour]) * BigDecimal("24")).floor(2).to_f.to_s)
		results[skuNo][:price_per_month] = sprintf('%.3f', (BigDecimal(results[skuNo][:price_per_day]) * BigDecimal("30")).floor(2).to_f.to_s)
	end
end

## reserved 
json_data['terms']['Reserved'].keys.each do |skuNo|
	if (results[skuNo])

		plans = json_data['terms']['Reserved'][skuNo].values.select do |plan|
			plan['termAttributes']['PurchaseOption'] == "All Upfront" # "All Upfront" のものだけ取得したい
		end

		results[skuNo][:price_reserved_1year_purchased_all_upfront] = sprintf('%.3f', plans.find { |plan|
			plan['termAttributes']['LeaseContractLength'] == '1yr'
		}['priceDimensions'].values.find {|priceDimension|
			priceDimension['description'] == "Upfront Fee"
		}['pricePerUnit']['USD'])

		results[skuNo][:price_reserved_3year_purchased_all_upfront] = sprintf('%.3f', plans.find { |plan|
			plan['termAttributes']['LeaseContractLength'] == '3yr'
		}['priceDimensions'].values.find {|priceDimension|
			priceDimension['description'] == "Upfront Fee"
		}['pricePerUnit']['USD'])

	end
end

# sort
sorted_result = {}
results.values.each do |row|
	sorted_result[row[:currentGeneration]] ||= {}
	sorted_result[row[:currentGeneration]][row[:instanceFamily]] ||= []
	sorted_result[row[:currentGeneration]][row[:instanceFamily]].push row
end

results = []
['Yes', 'No'].each do |currentGeneration| # 現行世代のものから並べる
	next unless sorted_result[currentGeneration]
	sorted_result[currentGeneration].keys.sort.each do |instanceFamily| # インスタンスファミリー毎に並べる
		results.concat sorted_result[currentGeneration][instanceFamily].sort_by { |row| row[:price_per_hour] }
	end
end

p results.to_json

上記を保存して以下のように実行する

curl https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonRDS/current/index.json > price-AmazonRDS.json
ruby rds.rb price-AmazonRDS.json | sed -e s/^\"// | sed -e s/\"$// | sed -e 's/\\"/"/g' | jq .

以下のような結果が取れる

[
  {
    "sku": "H7JQN46Z6VDZ3K5V",
    "location": "Asia Pacific (Tokyo)",
    "instanceType": "db.t2.small",
    "instanceFamily": "General purpose",
    "vcpu": "1",
    "physicalProcessor": "Intel Xeon Family",
    "clockSpeed": "Up to 3.3 GHz",
    "memory": "2 GiB",
    "networkPerformance": "Low to Moderate",
    "currentGeneration": "Yes",
    "price_unit": "USD",
    "price_per_hour": "0.060",
    "price_per_day": "1.440",
    "price_per_month": "43.200",
    "price_reserved_1year_purchased_all_upfront": "403.000",
    "price_reserved_3year_purchased_all_upfront": "776.000"
  },
  {
    "sku": "MK8ETWDCPSK52PEV",
    "location": "Asia Pacific (Tokyo)",
    "instanceType": "db.t2.medium",
    "instanceFamily": "General purpose",
    "vcpu": "2",
    "physicalProcessor": "Intel Xeon Family",
    "clockSpeed": "Up to 3.3 GHz",
    "memory": "4 GiB",
    "networkPerformance": "Low to Moderate",
    "currentGeneration": "Yes",
    "price_unit": "USD",
    "price_per_hour": "0.120",
    "price_per_day": "2.880",
    "price_per_month": "86.400",
    "price_reserved_1year_purchased_all_upfront": "792.000",
    "price_reserved_3year_purchased_all_upfront": "1530.000"
  },
  {
    "sku": "8Z6GS5F6NKX37Q5E",
    "location": "Asia Pacific (Tokyo)",
    "instanceType": "db.r3.large",
    "instanceFamily": "Memory optimized",
    "vcpu": "2",
    "physicalProcessor": "Intel Xeon E5-2670 v2 (Ivy Bridge)",
    "clockSpeed": "2.5 GHz",
    "memory": "15.25 GiB",
    "networkPerformance": "Moderate",
    "currentGeneration": "Yes",
    "price_unit": "USD",
    "price_per_hour": "0.350",
    "price_per_day": "8.400",
    "price_per_month": "252.000",
    "price_reserved_1year_purchased_all_upfront": "1704.000",
    "price_reserved_3year_purchased_all_upfront": "3433.000"
  },
  {
    "sku": "PQP78BGE4C2HXDQF",
    "location": "Asia Pacific (Tokyo)",
    "instanceType": "db.r3.xlarge",
    "instanceFamily": "Memory optimized",
    "vcpu": "4",
    "physicalProcessor": "Intel Xeon E5-2670 v2 (Ivy Bridge)",
    "clockSpeed": "2.5 GHz",
    "memory": "30.5 GiB",
    "networkPerformance": "Moderate",
    "currentGeneration": "Yes",
    "price_unit": "USD",
    "price_per_hour": "0.700",
    "price_per_day": "16.800",
    "price_per_month": "504.000",
    "price_reserved_1year_purchased_all_upfront": "3408.000",
    "price_reserved_3year_purchased_all_upfront": "6867.000"
  },
  {
    "sku": "2WTMTR9HDDT7AA73",
    "location": "Asia Pacific (Tokyo)",
    "instanceType": "db.r3.2xlarge",
    "instanceFamily": "Memory optimized",
    "vcpu": "8",
    "physicalProcessor": "Intel Xeon E5-2670 v2 (Ivy Bridge)",
    "clockSpeed": "2.5 GHz",
    "memory": "61 GiB",
    "networkPerformance": "High",
    "currentGeneration": "Yes",
    "price_unit": "USD",
    "price_per_hour": "1.400",
    "price_per_day": "33.600",
    "price_per_month": "1008.000",
    "price_reserved_1year_purchased_all_upfront": "6815.000",
    "price_reserved_3year_purchased_all_upfront": "13733.000"
  },
  {
    "sku": "VRNJP9SPPRH2KM8M",
    "location": "Asia Pacific (Tokyo)",
    "instanceType": "db.r3.4xlarge",
    "instanceFamily": "Memory optimized",
    "vcpu": "16",
    "physicalProcessor": "Intel Xeon E5-2670 v2 (Ivy Bridge)",
    "clockSpeed": "2.5 GHz",
    "memory": "122 GiB",
    "networkPerformance": "High",
    "currentGeneration": "Yes",
    "price_unit": "USD",
    "price_per_hour": "2.800",
    "price_per_day": "67.200",
    "price_per_month": "2016.000",
    "price_reserved_1year_purchased_all_upfront": "13631.000",
    "price_reserved_3year_purchased_all_upfront": "27466.000"
  },
  {
    "sku": "NC3BZ293ZJFBVUT5",
    "location": "Asia Pacific (Tokyo)",
    "instanceType": "db.r3.8xlarge",
    "instanceFamily": "Memory optimized",
    "vcpu": "32",
    "physicalProcessor": "Intel Xeon E5-2670 v2 (Ivy Bridge)",
    "clockSpeed": "2.5 GHz",
    "memory": "244 GiB",
    "networkPerformance": "10 Gigabit",
    "currentGeneration": "Yes",
    "price_unit": "USD",
    "price_per_hour": "5.600",
    "price_per_day": "134.400",
    "price_per_month": "4032.000",
    "price_reserved_1year_purchased_all_upfront": "27261.000",
    "price_reserved_3year_purchased_all_upfront": "54932.000"
  }
]
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?