无法在exports.handler(/var/task/index.js:4:34)上读取未定义的属性“ 0”

时间:2019-05-21 09:47:55

标签: node.js aws-lambda-edge

当我尝试执行函数引发错误时,我已经创建了一个Lambda函数

'use strict';

exports.handler = (event, context, callback) => {

  const request = event.Records[0].cf.request;
  const headers = request.headers;
  const origin = request.origin;
  const client_IP = request.clientIp;
  console.log(JSON.stringify(event));

  //Setup the two different origins
  const originA = "cloudfront-S3-origin1";
  const originB = "Cloudfront-S3-origin2";
  if (client_IP == "xx.xx.xx.xx") {
    origin.s3.domainName = originA;
  } else {
    origin.s3.domainName = originB;
  }
  callback(null, request);
};

2 个答案:

答案 0 :(得分:0)

您的Lambda函数可以通过手动触发,或者以某种方式错过了event.Records。我建议您在使用

之类的东西之前先检查可用数据。
'use strict';

exports.handler = (event, context, callback) => {
  if(!event.Records) {
    return callback(new Error('Records not available'));
  }

  const request = event.Records[0].cf.request;
  const headers = request.headers;
  const origin = request.origin;
  const client_IP = request.clientIp;
  console.log(JSON.stringify(event));

  //Setup the two different origins
  const originA = "cloudfront-S3-origin1";
  const originB = "Cloudfront-S3-origin2";
  if (client_IP == "xx.xx.xx.xx") {
    origin.s3.domainName = originA;
  } else {
    origin.s3.domainName = originB;
  }
  callback(null, request);
}; 

答案 1 :(得分:0)

我相信您是在手动触发lambda函数,而不是通过分配的事件触发器(s3,sns等)来触发。 对于手动触发,您需要自己传递事件,如下所示,我使用的是s3中的事件示例(如果您不手动执行,请告诉我如何触发它)。 另外,请确保您收到的事件是一个对象而不是字符串。

let event = {
  "Records": [
    {
      "eventVersion": "2.0",
      "eventSource": "aws:s3",
      "awsRegion": "us-west-2",
      "eventTime": "2018-04-20T21:01:59.672Z",
      "eventName": "ObjectCreated:Put",
      "userIdentity": {
        "principalId": "AWS:xxxxxxxxxxx:xxx"
      },
      "requestParameters": {
        "sourceIPAddress": "172.0.0.1"
      },
      "responseElements": {
        "x-amz-request-id": "xxxxxxxxxxx",
        "x-amz-id-2": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
      },
      "s3": {
        "s3SchemaVersion": "1.0",
        "configurationId": "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxxxxxxx",
        "bucket": {
          "name": "testing-process",
          "ownerIdentity": {
            "principalId": "xxxxxxxxxxxxxxx"
          },
          "arn": "arn:aws:s3:::testing-process"
        },
        "object": {
          "key": "5a2a37db9b15036253a4336d/1kmultipayables_together_chunk_2.csv",
          "size": 193,
          "eTag": "0ae33a40954ee4c745ea39f7e5e3d830",
          "sequencer": "005ADA554796220809"
        }
      }
    }
  ]
}
exports.handler = (event, context, callback) => {

  const request = event.Records[0].cf.request;
  const headers = request.headers;
  const origin = request.origin;
  const client_IP = request.clientIp;
  console.log(JSON.stringify(event));

  //Setup the two different origins
  const originA = "cloudfront-S3-origin1";
  const originB = "Cloudfront-S3-origin2";
  if (client_IP == "xx.xx.xx.xx") {
    origin.s3.domainName = originA;
  } else {
    origin.s3.domainName = originB;
  }
  callback(null, request);
};
module.exports.handler(event, null, function (err, res) {
    if (err) {
        console.log(err);
    } else {
        console.log(res);
    }
});````