boymeetscode
boymeetscode

Reputation: 825

AWS lambda never completes but doesn't appear to timeout either

I'm attempting to create a simple application. A user emails an attachment to a special inbox, AWS SES gets the email and stores it in S3, a lambda function is triggered and the lambda finds the email in S3, parses out the attachment (in this case a jpg) and then stores it in a different S3 bucket. Finally, the application creates a new row in an Airtable with the image as an attachment.

When I invoked this function locally using serverless, everything works fine. The email with the image are already stored in an S3 bucket so I've created a mock which passes the key explicitly to my lambda. However, when I deploy the application and send a test email, the following happens:

Before the email can be found and the attachment striped off and stored in a seperate S3 bucket the function just stops. No error message or timeout. It just stops. Cloudwatch logs look like the following:

START RequestId: 6a0364ae-0879-4ee1-9dcd-c8747de1a650 Version: $LATEST
2020-02-17T07:39:55.465Z    6a0364ae-0879-4ee1-9dcd-c8747de1a650    INFO    {
  s3SchemaVersion: '1.0',
  configurationId: 'emailtoairtable-dev-parse-224c3963d3a3f9c35018ae93a9fffea4',
  bucket: {
    name: 'to-airtable-temp',
    ownerIdentity: { principalId: 'AI5RGHFD5AFHE' },
    arn: 'arn:aws:s3:::to-airtable-temp'
  },
  object: {
    key: 'mtvuuiokqj55l2a8b0qser7tn9dhfignoh9c1vg1',
    size: 3804230,
    eTag: 'c821fb0a2a9c3b060e20e7d177f8b972',
    sequencer: '005E4A434810147365'
  }
}
2020-02-17T07:39:55.465Z    6a0364ae-0879-4ee1-9dcd-c8747de1a650    INFO    key mtvuuiokqj55l2a8b0qser7tn9dhfignoh9c1vg1
2020-02-17T07:39:55.465Z    6a0364ae-0879-4ee1-9dcd-c8747de1a650    INFO    Key pushed to list.  mtvuuiokqj55l2a8b0qser7tn9dhfignoh9c1vg1
END RequestId: 6a0364ae-0879-4ee1-9dcd-c8747de1a650
REPORT RequestId: 6a0364ae-0879-4ee1-9dcd-c8747de1a650  Duration: 1113.17 ms    Billed Duration: 1200 ms    Memory Size: 1024 MB    Max Memory Used: 114 MB Init Duration: 119.56 ms

Here is my handler.js file:

'use strict';

module.exports.parse = async event => {
  try {
    const aws = require('aws-sdk');
    const s3 = new aws.S3();
    const simpleParser = require('mailparser').simpleParser;
    const Airtable = require('airtable');
    const dateformat = require('dateformat');
    var base = new Airtable({ apiKey: process.env.airtableApiKey}).base(process.env.airtableBaseId);
    var data = [];
    var keys = [];

    event["Records"].forEach(async record => {
      console.log(record["s3"]);
      console.log('key', record["s3"]["object"]["key"]);
      keys.push(record["s3"]["object"]["key"]);
      console.log('Key pushed to list. ', record["s3"]["object"]["key"]);  // <-- this is the last line that I am sure processes because I see it in the CloudWatch logs.
      var temp_data = await s3.getObject(
      {
        Bucket: 'to-airtable-temp', 
        Key: record["s3"]["object"]["key"]
      }).promise();
      console.log('temp_data', temp_data);
      data.push(temp_data);
    });

    setTimeout( async function() {
      // console.log('data', data[0].Body.toString());
      let parsed = await simpleParser(data[0].Body.toString());
      console.log(parsed);
      // save the file to a public S3 bucket so it can be uploaded to airtable
      parsed["attachments"].forEach(function(attachment) {
        let now = new Date();
        s3.upload({
          Bucket: 'to-airtable-images',
          Key: keys[0] + dateformat(now, "yyyy-mm-dd") + '.jpg',
          Body: attachment.content,
          ContentType: "image/jpeg"
        },
        function(error, data) {
          if (error) {
            throw error;
          }
          console.log('File uploaded successfully. ' +data.Location);
          // Now upload to airtable
          base('Input').create([
            {
              "fields": {"Attachments": [
                {
                  "url": data.Location
                }
              ]}
            }
          ], function(err, records) {
            if (err) {
              console.error(err);
              return;
            }
            records.forEach(function (record) {
              console.log(record.getId());
            });
          });
        });
      });

      return {
        statusCode: 200,
        body: JSON.stringify(
          {
            message: 'Go Serverless v1.0! Your function executed successfully!',
            input: event,
            data: JSON.stringify(data),
          },
          null,
          2
        ),
      };
    }, 500);  // I've tried increasing this time but it still hangs.
  } catch (error) {
    console.error(error);
  }
};

Upvotes: 1

Views: 232

Answers (1)

Jason Wadsworth
Jason Wadsworth

Reputation: 8887

You shouldn't use async/await with the forEach function. Using async/await with a forEach loop. Instead, use the more modern for of syntax:

for (let record of event["Records"]) {
   // you can include await calls in this block
}

Upvotes: 0

Related Questions