Inside the lambda that I use to periodically register with the service, I check the value of the result from the server, and I want this value to be published in the AWS cloud as an indicator for forming a string.
I cannot depict how this is done in my life. 2 hours of combing around AWS documents does not result anywhere. Is it possible?
To be clear, this is not an O lambda metric; it is a published metric from lamdba.
the code:
'use strict';
const https = require('http');
exports.handler = (event, context, callback) => {
const now = new Date()
const yesterday = new Date(now.toISOString())
yesterday.setTime(now.getTime() - (1000 * 60 * 60 * 24));
const params = [
['limit',0],
['pageStart',0],
['startsOnOrAfter',encodeURIComponent(yesterday.toISOString())],
['startsOnOrBefore',encodeURIComponent(now.toISOString())]
].map(kv => `${kv[0]}=${kv[1]}&`).reduce((s1,s2) => s1.concat(s2))
var uri = `http://service/query?${params}`
const req = https.request(uri, (res) => {
let body = '';
res.setEncoding('utf8');
res.on('data', (chunk) => body += chunk);
res.on('end', () => {
if (!res.headers[ 'content-type' ].match('application/.*?json')) {
return callback(`unknown content type ${res.headers[ 'content-type' ]}`,body)
}
body = JSON.parse(body);
if(body.total && body.total > 0) {
callback(null, body.total);
}
else {
callback({
message: 'No plans found for time period',
uri: uri
})
}
});
});
req.on('error', callback);
req.end();
};
source
share