Last active
November 27, 2015 03:05
-
-
Save horike37/149d0fc4587e03a92f62 to your computer and use it in GitHub Desktop.
CloudWatchのメトリックを取得するLambdaファンクション
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
var AWS = require('aws-sdk'); | |
function ISODateString(d){ | |
function pad(n){return n<10 ? '0'+n : n} | |
return d.getUTCFullYear()+'-' | |
+ pad(d.getUTCMonth()+1)+'-' | |
+ pad(d.getUTCDate())+'T' | |
+ pad(d.getUTCHours())+':' | |
+ pad(d.getUTCMinutes())+':' | |
+ pad(d.getUTCSeconds())+'Z' | |
} | |
function getTimeStamp(d){ | |
var now = ISODateString(d); | |
return now; | |
} | |
function getMetricStatics(event, context) { | |
var cloudwatch = new AWS.CloudWatch(); | |
var past = new Date(); | |
past = new Date(past.setHours(past.getHours() - 1)); | |
var now = new Date(); | |
var endtime = getTimeStamp(now); | |
var starttime = getTimeStamp(past); | |
var params = { | |
EndTime: endtime, | |
MetricName: 'CPUUtilization', | |
Namespace: 'AWS/EC2', | |
Period: 300, | |
StartTime: starttime, | |
Statistics: [ | |
'Average' | |
], | |
Dimensions: [ | |
{ | |
Name: 'InstanceId', | |
Value: event.instance_id | |
} | |
] | |
}; | |
cloudwatch.getMetricStatistics(params, function(err, data) { | |
if (err) console.log(err, err.stack); | |
else context.done(null, JSON.stringify(data)); | |
}); | |
} | |
exports.handler = function(event, context) { | |
getMetricStatics(event, context); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment