Skip to content

Instantly share code, notes, and snippets.

@mooyoul
Created May 15, 2017 17:11
Show Gist options
  • Save mooyoul/6838f8b4624c33f5a03b5357946dd370 to your computer and use it in GitHub Desktop.
Save mooyoul/6838f8b4624c33f5a03b5357946dd370 to your computer and use it in GitHub Desktop.
CloudFront Metrics via CloudWatch API
'use strict';
const AWS = require('aws-sdk');
const cloudwatch = new AWS.CloudWatch();
const endDate = new Date();
const startDate = new Date(endDate.getTime() - (3600 * 24 * 1000)); // 1 day ago
cloudwatch.getMetricStatistics({
Namespace: 'AWS/CloudFront',
MetricName: 'Requests',
StartTime: startDate,
Period: 60 * 60, // per each 1 hour period
EndTime: endDate,
Dimensions: [
{ Name: 'Region', Value: 'Global' },
{ Name: 'DistributionId', Value: 'TARGET_CLOUDFRONT_DIST_ID' },
],
Statistics: ['Sum'],
}, (e, data) => {
if (e) { // handle error if an error occurred
console.error(e);
return;
}
console.log(data);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment