Skip to content

Instantly share code, notes, and snippets.

@hmelenok
Created July 19, 2020 18:44
Show Gist options
  • Save hmelenok/c0f72e7b4a14e31bde65c6fe27192b96 to your computer and use it in GitHub Desktop.
Save hmelenok/c0f72e7b4a14e31bde65c6fe27192b96 to your computer and use it in GitHub Desktop.
/* Amplify Params - DO NOT EDIT
You can access the following resource attributes as environment variables from your Lambda function
var environment = process.env.ENV
var region = process.env.REGION
var storageEmailsBucketName = process.env.STORAGE_EMAILS_BUCKETNAME
Amplify Params - DO NOT EDIT */
var storageEmailsBucketName = process.env.STORAGE_EMAILS_BUCKETNAME;
var AWS = require('aws-sdk');
var s3 = new AWS.S3();
exports.handler = async (event, context, callback) => {
console.log('Email capture');
var sesNotification = event.Records[0].ses;
var destination = sesNotification.mail.destination[0];
var messageId = sesNotification.mail.messageId;
console.log('SES Notification:\n', JSON.stringify(sesNotification, null, 2));
console.log('bucket:\n', storageEmailsBucketName);
console.log('filekey:\n', `${destination}/${messageId}`);
try {
const status = await s3.putObject({
Bucket: storageEmailsBucketName,
Key: `${destination}/${messageId}`,
Body: JSON.stringify(sesNotification),
ContentType: 'text/plain',
}, function (err) {
if (err) {
throw err;
}
}).promise();
console.log('S3 status:', status);
} catch (e) {
console.log('ERROR', JSON.stringify(e));
return {
statusCode: 500,
error: e
};
}
// TODO implement
const response = {
statusCode: 200,
body: JSON.stringify(sesNotification)
};
return response;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment