Skip to content

Instantly share code, notes, and snippets.

@y13i
Last active June 28, 2017 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save y13i/2b2da2c3f4f9a5c119f29f21c1ce5e3b to your computer and use it in GitHub Desktop.
Save y13i/2b2da2c3f4f9a5c119f29f21c1ce5e3b to your computer and use it in GitHub Desktop.
AWS Price List API で雑に月額を出してみる ref: http://qiita.com/y13i/items/b296844486363946d400
const region = process.env["AWS_REGION"] || "us-east-1";
const instanceType = process.env["INSTANCE_TYPE"] || "c4.large";
const tenancy = process.env["TENANCY"] || "Shared";
const operatingSystem = process.env["OPERATING_SYSTEM"] || "Linux";
const offerType = process.env["OFFER_TYPE"] || "OnDemand";
// ↑フィルター条件をenvで指定してね
const axios = require("axios");
const api = axios.create({
baseURL: "https://pricing.us-east-1.amazonaws.com"
});
Promise.resolve().then(() => {
return api.get("/offers/v1.0/aws/AmazonEC2/current/region_index.json")
}).then(regionIndexResponse => {
const currentVersionUrl = regionIndexResponse.data.regions[region].currentVersionUrl;
return api.get(currentVersionUrl);
}).then(currentVersionResponse => {
const filteredProducts = [];
for (productCode in currentVersionResponse.data.products) {
const product = currentVersionResponse.data.products[productCode];
if (product.attributes.instanceType !== instanceType) continue;
if (product.attributes.tenancy !== tenancy) continue;
if (product.attributes.operatingSystem !== operatingSystem) continue;
filteredProducts.push(product);
}
if (filteredProducts.length > 1) {
console.log(`絞りきれなかったかも… (${filteredProducts.length} products)`);
process.exit(1);
};
const offers = currentVersionResponse.data.terms[offerType][filteredProducts[0].sku];
if (Object.keys(offers).length > 1) {
console.log(`絞りきれなかったかも… (${Object.keys(offers).length} offers)`);
process.exit(1);
};
const offer = offers[Object.keys(offers)[0]];
const priceDimension = offer.priceDimensions[Object.keys(offer.priceDimensions)[0]];
const pricePerUnit = parseFloat(priceDimension.pricePerUnit.USD);
const monthlyPrice = pricePerUnit * 24 * 30;
console.log(JSON.stringify({
region,
instanceType,
tenancy,
operatingSystem,
monthlyPrice,
description: priceDimension.description,
}, undefined, 2))
}).catch(e => console.log(e));
$ curl -I https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonEC2/20170605233259/ap-northeast-1/index.json
HTTP/1.1 200 OK
Content-Type: application/octet-stream
Content-Length: 10293063
Connection: keep-alive
Date: Mon, 19 Jun 2017 04:27:13 GMT
Last-Modified: Tue, 06 Jun 2017 00:55:29 GMT
ETag: "35bdcd83d7f7863f5f26550206b0e624"
Accept-Ranges: bytes
Server: AmazonS3
Age: 24112
X-Cache: Hit from cloudfront
Via: 1.1 41f313008af830d498dcb13814523bd7.cloudfront.net (CloudFront)
X-Amz-Cf-Id: 1oUuTPzPmELzB65fwzB1ds1ezCEpBn8ZWVANIJT-rURfG2p8buOFMg==
$ node ec2ZatsuPrice.js
{
"region": "us-east-1",
"instanceType": "c4.large",
"tenancy": "Shared",
"operatingSystem": "Linux",
"monthlyPrice": 72.00000000000001,
"description": "$0.1 per On Demand Linux c4.large Instance Hour"
}
$ AWS_REGION=us-west-2 INSTANCE_TYPE=r3.xlarge node ec2ZatsuPrice.js
{
"region": "us-west-2",
"instanceType": "r3.xlarge",
"tenancy": "Shared",
"operatingSystem": "Linux",
"monthlyPrice": 239.76000000000002,
"description": "$0.333 per On Demand Linux r3.xlarge Instance Hour"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment