Search code examples
node.jsamazon-web-servicesjimp

Jimp not reading from url in lambda trigger at aws


Log from aws cloudwatch

20:42:36
START RequestId: 3b39ddb6-f2f5-4e11-a3d6-59f47f16240b Version: $LATEST
20:42:39
END RequestId: 3b39ddb6-f2f5-4e11-a3d6-59f47f16240b
20:42:39
REPORT RequestId: 3b39ddb6-f2f5-4e11-a3d6-59f47f16240b Duration: 3003.18 ms Billed Duration: 3000 ms Memory Size: 128 MB Max Memory Used: 128 MB Init Duration: 703.55 ms
REPORT RequestId: 3b39ddb6-f2f5-4e11-a3d6-59f47f16240b  Duration: 3003.18 ms    Billed Duration: 3000 ms    Memory Size: 128 MB Max Memory Used: 128 MB Init Duration: 703.55 ms    
20:42:39
2020-01-06T15:12:39.657Z 3b39ddb6-f2f5-4e11-a3d6-59f47f16240b Task timed out after 3.00 seconds
2020-01-06T15:12:39.657Z 3b39ddb6-f2f5-4e11-a3d6-59f47f16240b Task timed out after 3.00 seconds

Node.js AWS Lambda code

const aws = require('aws-sdk');
const Jimp = require("jimp");
const uuid = require("uuid/v4");
const s3 = new aws.S3();
//lambda trigger handler for triggering event after object being uploaded into bucket
exports.handler = async (event, context) => {
  const key = event.Records[0].s3.object.key; // Uploaded object key
  const sanitizedKey = key.replace(/\+/g, ' ');
  const keyWithoutExtension = sanitizedKey.replace(/.[^.]+$/, '');
  const objectKey = keyWithoutExtension+'_mb.';

//read object using jimp to resize it accordingly
  const image = await Jimp.read(prefix+key)
                      .then((image) => {
                        console.log( "Before resizing" , image)
                        return image
                          .resize(256, 256) // resize
                          .quality(90) // set JPEG quality
                      })
                     .then((image) => {
                      return uploadToS3(image, objectKey+image.getExtension(), image.getExtension());
                      })
                      .catch(err => {
                        throw err;
                      })
                      .finally(() => {
                        console.info("Function ran successfully")
                      })
  console.log(image);
  return image
}
//upload file to s3 after resizing
async function uploadToS3(data, key, ContentType) {
  console.log("Inside uploadToS3: ", data, key, ContentType)
  const resp = await s3
    .putObject({
      Bucket: Bucket,
      Key: key,
      Body: data,
      ContentType: ContentType
    })}
  console.log("Response from S3: ", resp);
  return resp
}

Solution

  • Everything looks fine apart from a small change in uploadToS3 method. Be default is works with callback pattern unless you do .promise() at the end. see the updated method

    //upload file to s3 after resizing
    async function uploadToS3(data, key, ContentType) {
      console.log("Inside uploadToS3: ", data, key, ContentType)
      const resp = await s3
        .putObject({
          Bucket: Bucket,
          Key: key,
          Body: data,
          ContentType: ContentType
        }).promise();
      console.log("Response from S3: ", resp);
      return resp
    }
    

    Also worth increasing your lambda timeout to somethign else apart from default 3 seconds just to rule out that it is timing out before operation complete.

    Hope this helps