Created
August 7, 2018 18:11
-
-
Save jbuck/0497eb62f4cda29ac4de76d6e0cc9e51 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const AWS = require('aws-sdk') | |
const main = async () => { | |
const env = process.argv[2] | |
const regions = { | |
'eu-west-1': { | |
data_processing_cost: 0.008, | |
lcu_cost: 0.008 | |
}, | |
'us-east-1':{ | |
data_processing_cost: 0.008, | |
lcu_cost: 0.008 | |
}, | |
'us-west-2': { | |
data_processing_cost: 0.008, | |
lcu_cost: 0.008 | |
} | |
} | |
const start = '2018-07', end = '2018-08' | |
console.log("Environment\tRegion\tLoad Balancer Name\tEstimated Data Processed\tEstimated ELB Cost\tEstimated LCUs Consumed\tEstimated ALB Cost") | |
for (const region of Object.keys(regions)) { | |
const CloudWatch = new AWS.CloudWatch({region: region}) | |
const ELB = new AWS.ELB({region: region}) | |
const lb_names = (await ELB.describeLoadBalancers({}).promise()) | |
.LoadBalancerDescriptions.map((lb) => { return lb.LoadBalancerName }) | |
for (const lb of lb_names) { | |
const results = await CloudWatch.getMetricData(cloudwatch_query(lb, start, end)).promise() | |
const data_processed = results.MetricDataResults[0].Values.reduce((accumlator, bytes) => { | |
return accumlator + (bytes / 1000000000) | |
}, 0) | |
const data_processed_cost = data_processed * regions[region].data_processing_cost | |
const lcus_consumed = results.MetricDataResults[1].Values.reduce((accumlator, lcus) => { | |
return accumlator + lcus | |
}, 0) | |
const lcus_cost = lcus_consumed * regions[region].lcu_cost | |
console.log(`${env}\t${region}\t${lb}\t${data_processed}\t${data_processed_cost}\t${lcus_consumed}\t${lcus_cost}`) | |
} | |
} | |
} | |
main(); | |
const cloudwatch_query = (elb, start_date, end_date) => { | |
const start = new Date(start_date) | |
const end = new Date(end_date) | |
return { | |
MetricDataQueries: [{ | |
Id: 'processed_bytes_query', | |
MetricStat: { | |
Metric: { | |
Dimensions: [{ | |
Name: 'LoadBalancerName', | |
Value: elb | |
}], | |
MetricName: 'EstimatedProcessedBytes', | |
Namespace: 'AWS/ELB' | |
}, | |
Period: 86400, | |
Stat: 'Sum', | |
Unit: 'Bytes' | |
} | |
}, { | |
Id: 'alb_consumed_lcus', | |
MetricStat: { | |
Metric: { | |
Dimensions: [{ | |
Name: 'LoadBalancerName', | |
Value: elb | |
}], | |
MetricName: 'EstimatedALBConsumedLCUs', | |
Namespace: 'AWS/ELB' | |
}, | |
Period: 86400, | |
Stat: 'Sum' | |
} | |
}], | |
StartTime: start, | |
EndTime: end | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment