1

i've read all other topics and tried a few answers, but i can't seem to figure out why i get this error. My code gets the uploaded pic in a S3 bucket, reduces the quality and puts it in a second bucket. Plain and simple. With small/medium images everything works just fine, but if i upload something over 2 MB (more or less) i get the error in the title. My Lambda function has 128MB and 3 minutes of timeout; here is the code:

const gm = require('gm').subClass({imageMagick: true});
const AWS = require('aws-sdk');
const async = require('async');
const S3 = new AWS.S3();

exports.handler = (event, context, callback) => {
    var srcBucket = event.Records[0].s3.bucket.name;    
    var srcKey = event.Records[0].s3.object.key;
    var dstBucket = "destinationbucket";
    var dstKey = "resized-" + srcKey;

     // Infer the image type.
    var typeMatch = srcKey.match(/\.([^.]*)$/);
    if (!typeMatch) {
        callback("Could not determine the image type.");
        return;
    }

    var imageType = typeMatch[1].toLowerCase();
    if (imageType != "jpg" && imageType != "png" && imageType != "jpeg") {
        callback('Unsupported image type: ${imageType}');
        return;
    }
    async.waterfall([
        function download(next) {
            S3.getObject({Bucket : srcBucket, Key : srcKey}, next);
        },
        function transform(response, next) {
            var img_quality_reduced = gm(response.Body);
            img_quality_reduced.quality(75).toBuffer(function( error, buffer )
                {
                    if( error ) { console.log( error ); return; }
                     next(null, response.ContentType, buffer);
                }
            );
        },
        function upload(contentType, data, next) {
            S3.putObject({Bucket: dstBucket, Key: dstKey, Body: data}, next);
        },
    function ending(next) {
        console.log('got to ending');
        context.done();
    }
    ], function (err) {
        console.log(err);
        context.done();
    });
};

Any idea why is this happening? i have loaded async, gm and graphicsmagick to Lambda (as a zip file). all downloaded through npm

  • Perhaps your server may have limitations on the file size that can be uploaded or on width or height. Also ImageMagick has a policy.xml file that you may edit to change ImageMagick environment limitations. I do not know if GraphicsMagick has a similar policy.xml. Sorry, I am not knowledgeable about AWS – fmw42 Jul 28 '17 at 22:43
  • 1
    Thanks for the suggestion. Turns out Lambda was just needing more RAM. – David Torreggiani Aug 02 '17 at 13:38

0 Answers0