运行lambda“ errorMessage”时出现以下错误:“无法读取未定义的属性” requestParameters“,

时间:2019-07-17 15:23:23

标签: javascript node.js amazon-web-services aws-lambda

我试图运行一个lambda来将AWS中的日志组的日志保留时间设置为14天,我从网络上提取了以下.js代码,但是当我运行它时,出现了错误"errorMessage": "Cannot read property 'requestParameters' of undefined",. Runtime of Nodejs is 8.10.

这是我正在运行的.js:

const AWS = require('aws-sdk');
const cloudwatchLogs = new AWS.CloudWatchLogs();

function setRetentionOfCloudwatchLogGroup(logGroupName, duration) {
    let params = {
        logGroupName : logGroupName,
        retentionInDays: duration
    };
    return cloudwatchLogs.putRetentionPolicy(params).promise();
}

exports.handler = async (event) => {
    const logGroupName = event.logGroupName ? event.logGroupName : event.detail.requestparameters.logGroupName;
    try {
        await setRetentionOfCloudwatchLogGroup(logGroupName, 14);
        console.log('Retention has been set to ' + logGroupName + 'for 2 weeks');
        return;
    } catch(error) {
        console.error(error);
        throw error;
    }
};

这是我遇到的错误:

Response:
{
  "errorType": "TypeError",
  "errorMessage": "Cannot read property 'requestParameters' of undefined",
  "trace": [
    "TypeError: Cannot read property 'requestParameters' of undefined",
    "    at Runtime.exports.handler (/var/task/index.js:14:81)",
    "    at Runtime.handleOnce (/var/runtime/Runtime.js:63:25)",
    "    at process._tickCallback (internal/process/next_tick.js:68:7)"
  ]
}

1 个答案:

答案 0 :(得分:0)

第14行的活动的detail属性未设置。不知道您是否希望事件在每个调用中都具有该功能,但是我认为在某些情况下不需要通过事件处理程序传递任何详细信息并且该属性保留为空。一个简单的空检查将解决此问题。