Simple AWS Cost Explorer API Implementation
npm install aws-cost-explorer
This module requires an AWS Key Pair and access to AWS Cost Explorer API (ce). Example policy to attach:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ce:*"
],
"Resource": [
"*"
]
}
]
}
source : Billing And Cost Management Example 12
var CostExplorer = require('aws-cost-explorer');
var ce = CostExplorer();
ce.getMonthToDateCosts(null, function(err, data) {
if (err) {
console.log(err);
} else {
console.dir(data, { depth: null });
}
});
var config = {
apiVersion: '2017-10-25',
accessKeyId : 'AKIAJVCSKEY3SAMPLE',
secretAccessKey : 'yWKUsVwtaXnssdGBeEVGPSAMPLE9hZuz9SAMPLE',
region : 'us-east-1'
}
var CostExplorer = require('aws-cost-explorer');
var ce = CostExplorer(config);
ce.getMonthToDateCosts(null, function(err, data) {
if (err) {
console.log(err);
} else {
console.dir(data, { depth: null });
}
});
aws-cost-explorer uses config module to automatically load aws config keys an values from _dirname/config folder
var CostExplorer = require('aws-cost-explorer');
var ce = CostExplorer();
ce.getMonthToDateCosts(null, function(err, data) {
if (err) {
console.log(err);
} else {
console.dir(data, { depth: null });
}
});
Sample default.json file located in _dirname/config folder
{
"AWS": {
"CostExplorer" : {
"apiVersion" : "2017-10-25",
"accessKeyId" : "AKIAJV1111SSDDDY73NTTEBVXQ",
"secretAccessKey" : "yWKUsVwtaXnASDqwe1232312L9DI9hZuz9Gp6cEXB",
"region" : "us-east-1"
}
}
}
opts = {
granularity : "MONTHLY" | "DAILY",
metrics : "BlendedCost" | "UnblendedCost"
}
{
ResultsByTime: [{
TimePeriod: {
Start: '2017-11-01',
End: '2017-11-30'
},
Total: {
BlendedCost: {
Amount: '4066.1336704447',
Unit: 'USD'
}
},
Groups: [],
Estimated: false
}],
Total: {
Amount: 4066,
Unit: 'USD'
}
}
npm tests
Sample Rest API using Express