1
votes

I would like to create Lambda function which will parse the emails which uploaded to S3 bucket through SES receipt rule.

Uploading to S3 bucket through SES receipt rule works fine. So, its tested already and confirmed that it uploads the file correctly.

My Amazon Lambda function:

var AWS = require('aws-sdk');
var s3 = new AWS.S3();

var bucketName = 'bucket_name/folder/destination';

exports.handler = function(event, context, callback) {
console.log('Process email');

var sesNotification = event.Records[0].ses;
console.log("SES Notification:\n", JSON.stringify(sesNotification, null, 2));

// Retrieve the email from your bucket
s3.getObject({
        Bucket: bucketName,
        Key: sesNotification.mail.messageId
    }, function(err, data) {
        if (err) {
            console.log(err, err.stack);
            callback(err);
        } else {
            console.log("Raw email:\n" + data.Body);

            // Custom email processing goes here

            callback(null, null);
        }
    });
};

When there is a file upload it triggers the lambda but I get an [SignatureDoesNotMatch] error:

{ "errorMessage": "The request signature we calculated does not match the signature you provided. Check your key and signing method.", "errorType": "SignatureDoesNotMatch", "stackTrace": [ "Request.extractError (/var/runtime/node_modules/aws-sdk/lib/services/s3.js:524:35)", "Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:105:20)", "Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:77:10)", "Request.emit (/var/runtime/node_modules/aws-sdk/lib/request.js:615:14)", "Request.transition (/var/runtime/node_modules/aws-sdk/lib/request.js:22:10)", "AcceptorStateMachine.runTo (/var/runtime/node_modules/aws-sdk/lib/state_machine.js:14:12)", "/var/runtime/node_modules/aws-sdk/lib/state_machine.js:26:10", "Request. (/var/runtime/node_modules/aws-sdk/lib/request.js:38:9)", "Request. (/var/runtime/node_modules/aws-sdk/lib/request.js:617:12)", "Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:115:18)" ] }

If anyone can help me to approach this problem, it will be great! Thanks

1
Most probably related to your bucketName variable. What does "destination" refer to in "bucket_name/folder/destination"?Khalid T.
@KhalidT. actually, bucketName variable represents the place where it should get the object.Tural Gulmammadov

1 Answers

1
votes

Okay I solved it. The bucketName should contain only the bucket's name. But the key can contain the rest of the route to your file's exact "directory".

So, basically bucketName should not contain subfolders but key should.

Thanks

For reference: AWS Lambda S3 GET/POST - SignatureDoesNotMatch error