3

I have created a Lambda Function when I try to execute the function throw error

'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);
};
Arif Khan
  • 5,039
  • 2
  • 16
  • 27
midun
  • 31
  • 1
  • 2
  • 1
    I don't know about lambda function, but it shows the error because the handler function is called with undefined parameter. that means no parameter is given. that's why event is undefined here and shows this error – Samiul Alam May 21 '19 at 10:01
  • I have the exact error - did you find a solution ? – dowi Aug 11 '20 at 18:33

3 Answers3

0

Your Lambda function either trigger by manually or somehow missed event.Records. I would like to recommend you to check data available before using something like

'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);
}; 
Arif Khan
  • 5,039
  • 2
  • 16
  • 27
  • i have tried the above code you have given and tested and it returns "Records not available" , so i have passed the JSON format Records to the Event and tried to test it now I'm getting and another Error TypeError: Cannot read property 's3' of undefined at exports.handler (/var/task/index.js:20:15), – midun May 22 '19 at 05:44
0

I believe you are triggering your lambda function manually instead of via the assigned event trigger(s3, sns etc). For manual triggering you need to pass the event by yourself, something like below, I am using an event sample from s3.(If you are not doing it manual let me know how you are triggering it.) Also, make sure the event that you have received is an object and not a string.

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);
    }
});````
Siddharth Yadav
  • 383
  • 2
  • 9
  • i have passed the Records in the Event and now I'm getting an another Error TypeError: Cannot read property 's3' of undefined at exports.handler (/var/task/index.js:20:15) – midun May 22 '19 at 05:45
  • how to define S3 in the Lambda – midun May 22 '19 at 05:50
  • I believe you have mixed the two samples, the one that I have provided is used when the lambda is triggered from an action in S3 bucket(ex- inserting a file). The example that you have used is of any other event. Could you please let me know how are you trying to trigger it. Also, you don't define an s3 in lambda, you associate a lambda with an already defined s3 bucket. For more info - https://docs.aws.amazon.com/AmazonS3/latest/user-guide/upload-objects.html – Siddharth Yadav May 22 '19 at 08:31
0

we can use in this way to handle such error:

 let payload = {};
    payload = event;
    if (event.Records) payload = JSON.parse(event.Records[0].body);
    if (typeof payload == "string") payload = JSON.parse(payload);