是否可以通过电子邮件发送与Cloudwatch中出现的Lambda函数相同的错误日志?
答案 0 :(得分:0)
要交付日志,您可以创建cloudwatch订阅过滤器,可以单击日志组,然后在AWS lambda上单击流并选择lambda函数。
日志将以gzip格式提供,您可以使用以下代码将其解压缩
var AWS = require('aws-sdk');
var zlib = require('zlib');
// Set the region
AWS.config.update({region: 'REGION'});
// Create sendTemplatedEmail params
var params = {
Destination: { /* required */
CcAddresses: [
'EMAIL_ADDRESS',
/* more CC email addresses */
],
ToAddresses: [
'EMAIL_ADDRESS',
/* more To email addresses */
]
},
Source: 'EMAIL_ADDRESS', /* required */
Template: 'TEMPLATE_NAME', /* required */
TemplateData: '{ \"REPLACEMENT_TAG_NAME\":\"REPLACEMENT_VALUE\" }', /* required */
ReplyToAddresses: [
'EMAIL_ADDRESS',
],
};
exports.handler = function(input, context) {
var payload = new Buffer(input.awslogs.data, 'base64');
zlib.gunzip(payload, function(e, result) {
if (e) {
context.fail(e);
} else {
result = JSON.parse(result.toString('ascii'));
console.log("Event Data:", JSON.stringify(result, null, 2));
var sendPromise = new AWS.SES({apiVersion: '2010-12-
01'}).sendTemplatedEmail(params).promise();
// Handle promise's fulfilled/rejected states
sendPromise.then(
function(data) {
console.log(data);
}).catch(
function(err) {
console.error(err, err.stack);
});
context.succeed();
}
});
};
此代码基本上是获取日志,将其解压缩并使用AWS SES通过电子邮件发送 要获取更多信息,请点击sdk。
cloudwatch log lambda subscription
Sending email using Simple email service sdk