Introduction to AWS Price list APIs

Posted By : Pulkit Chadha | 20-Jun-2017

Introduction:

The AWS Price List API help us to get prices of AWS services. We can subscribe to Amazon SNS to get notification when prices for the AWS services change.

1) Offer index file – A file which contains all the available AWS services, with a URL for each offer file where you can download pricing details.To download the offer index file, go to the following URL:

https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/index.json 
        

2) Offer file – A file which contains all the products and prices a AWS service in all regions or in a specific region.For example:

https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/{{ServiceCode}}/current/index.json
        

Note :  Offer index file can be downloaded in JSON format or CSV format.To download CSV file, replace .json extension in the offer file url with .csv. If we want to download offer file for a specific service, just replace the ServiceCode in the url with the service code.
For example, to download  Amazon ETS offer file for US West (Oregon), use the following URL:

https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonETS/current/us-west-2/index.json
       

Step 1: We need Request package in our application to get JSON response from AWS Pricing API.we can install it using the command:

npm install request
	or
npm install request --save //save to package.json
        

Step 2: Write the code given below:

var request = require('request');
var _ = require('underscore');

var AWS_S3_Price_API = 'https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonS3/current/index.json';
var StorageType = 'Standard - Infrequent Access';

var regions = {
	"us-east-1": "US East (N. Virginia)",
	"us-east-2": "US East (Ohio)",
	"us-west-1": "US West (N. California)",
	"us-west-2": "US West (Oregon)",
	"ca-central-1": "Canada (Central)",
	"eu-west-1": "EU (Ireland)",
	"eu-central-1": "EU (Frankfurt)",
	"eu-west-2": "EU (London)",
	"ap-northeast-1": "Asia Pacific (Tokyo)",
	"ap-northeast-2": "Asia Pacific (Seoul)",
	"ap-southeast-1": "Asia Pacific (Singapore)",
	"ap-southeast-2": "Asia Pacific (Sydney)",
	"ap-south-1": "Asia Pacific (Mumbai)",
	"sa-east-1": "South America (Sao Paulo)",
	"us-gov-west-1": "US Gov West 1",
	"us-gov-west-1": "AWS GovCloud (US)",
	'NA': 'Any'
}

function getPriceBySku(sku, data) {
	var prices = data.terms.OnDemand;

	var pricesSKU = Object.keys(prices);
	var outer = prices[sku];
	var inner = Object.keys(prices[sku])[0];
	var priceDimensions = outer[inner].priceDimensions;
	var innner3 = Object.keys(priceDimensions)[0];
	var innner4 = priceDimensions[innner3];
	var pricePerUnit = innner4.pricePerUnit.USD;

	return pricePerUnit;
}

function getRegionByLocation(location, regions) {
	var invertedRegions = _.invert(regions);
	return invertedRegions[location];
}

function AWSStoragePriceParser(data) {
	var products = data.products;
	var productSKU = Object.keys(products);
	var priceArr = [];
	for (var i = 0; i < productSKU.length; i++) {
		var product = products[productSKU[i]];
		if (product.attributes.volumeType == StorageType) {
			var obj = {
				location: getRegionByLocation(product.attributes.location, regions),
				storagePrice: getPriceBySku(product.sku, data)
			}
			priceArr.push(obj);
		}
	}
	return priceArr;
}

request(AWS_S3_Price_API, function (error, response, data) {
	if (error) {
		console.log('error:', error); 
	} else {
		var finalJSON = AWSStoragePriceParser(JSON.parse(data));// AWS S3 region wise price
		console.log('Price JSON', finalJSON);
	}
});

        THANKS

About Author

Author Image
Pulkit Chadha

Pulkit is an expert web app developer and has good knowledge of JQuery, MongoDb, NodeJs, AngularJS, kaltura, Akamai. etc.

Request for Proposal

Name is required

Comment is required

Sending message..